blob: 38d2699ee29cf2b00fae8ab4fdb607091b502bf8 [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
Ian Rogers60db5ab2012-02-20 17:02:00 -0800134extern void* FindNativeMethod(Thread* self) {
135 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700136
Ian Rogers60db5ab2012-02-20 17:02:00 -0800137 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700138 DCHECK(method != NULL);
139
140 // Lookup symbol address for method, on failure we'll return NULL with an
141 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -0800142 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700143 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800144 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700145 return NULL;
146 } else {
147 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -0800148 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700149 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 Rogers60db5ab2012-02-20 17:02:00 -0800513static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
514 intptr_t value = *arg_ptr;
515 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
516 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
517 CHECK(Heap::IsHeapAddress(value_as_work_around_rep));
518 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
519}
520
521extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
522 DCHECK(Thread::Current() == self);
523 // TODO: this code is specific to ARM
524 // On entry the stack pointed by sp is:
525 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
526 // | LR |
527 // | R3 | arg2
528 // | R2 | arg1
529 // | R1 | jclass/jobject
530 // | R0 | JNIEnv
531 // | unused |
532 // | unused |
533 // | unused | <- sp
534 Method* jni_method = self->GetTopOfStack().GetMethod();
535 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
536 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
537 // Fix up this/jclass argument
538 WorkAroundJniBugsForJobject(arg_ptr);
539 arg_ptr++;
540 // Fix up jobject arguments
541 MethodHelper mh(jni_method);
542 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800543 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800544 char shorty_char = mh.GetShorty()[i];
545 if (shorty_char == 'L') {
546 WorkAroundJniBugsForJobject(arg_ptr);
547 }
548 if (shorty_char == 'J' || shorty_char == 'D') {
549 if (reg_num == 2) {
550 arg_ptr = sp + 8; // skip to out arguments
551 reg_num = -1;
552 } else if (reg_num == 3) {
553 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
554 reg_num = -1;
555 } else {
556 DCHECK(reg_num == -1);
557 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
558 arg_ptr += 3; // unaligned, pad and move through stack arguments
559 } else {
560 arg_ptr += 2; // aligned, move through stack arguments
561 }
562 }
563 } else {
564 if (reg_num == 2) {
565 arg_ptr++; // move through register arguments
566 reg_num++;
567 } else if (reg_num == 3) {
568 arg_ptr = sp + 8; // skip to outgoing stack arguments
569 reg_num = -1;
570 } else {
571 DCHECK(reg_num == -1);
572 arg_ptr++; // move through stack arguments
573 }
574 }
575 }
576 // Load expected destination, see Method::RegisterNative
577 return reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
578}
579
580
Ian Rogerscaab8c42011-10-12 12:11:18 -0700581// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800582static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
583 size_t expected_size) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800584 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700585 if (UNLIKELY(resolved_field == NULL)) {
586 return NULL;
587 }
588 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800589 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700590 if (UNLIKELY(!fields_class->IsInitializing())) {
591 return NULL;
592 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800593 Class* referring_class = referrer->GetDeclaringClass();
594 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
595 !referring_class->CanAccessMember(fields_class,
596 resolved_field->GetAccessFlags()))) {
597 // illegal access
598 return NULL;
599 }
600 FieldHelper fh(resolved_field);
601 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
602 fh.FieldSize() != expected_size)) {
603 return NULL;
604 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700605 return resolved_field;
606}
607
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800608
Ian Rogerscaab8c42011-10-12 12:11:18 -0700609// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800610Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
611 bool is_static, bool is_primitive, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700612 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700613 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800614 if (UNLIKELY(resolved_field == NULL)) {
615 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
616 return NULL; // failure
617 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700618 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800619 Class* referring_class = referrer->GetDeclaringClass();
620 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800621 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800622 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800623 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
624 resolved_field->GetAccessFlags()))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800625 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
Ian Rogers1bddec32012-02-04 12:27:34 -0800626 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800627 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800628 FieldHelper fh(resolved_field);
629 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
630 fh.FieldSize() != expected_size)) {
631 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
632 "Attempted read of %zd-bit %s on field '%s'",
633 expected_size * (32 / sizeof(int32_t)),
634 is_primitive ? "primitive" : "non-primitive",
635 PrettyField(resolved_field, true).c_str());
636 return NULL; // failure
637 } else if (!is_static) {
638 // instance fields must be being accessed on an initialized class
Ian Rogers1bddec32012-02-04 12:27:34 -0800639 return resolved_field;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800640 } else {
641 // If the class is already initializing, we must be inside <clinit>, or
642 // we'd still be waiting for the lock.
643 if (fields_class->IsInitializing()) {
644 return resolved_field;
645 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
646 return resolved_field;
647 } else {
648 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
649 return NULL; // failure
650 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800651 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700652 }
653 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700654}
655
Ian Rogersce9eca62011-10-07 17:11:03 -0700656extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
657 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800658 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700659 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800660 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700661 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700662 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800663 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
664 if (LIKELY(field != NULL)) {
665 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700666 }
667 return 0; // Will throw exception by checking with Thread::Current
668}
669
670extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
671 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800672 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700673 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800674 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700675 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700676 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800677 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
678 if (LIKELY(field != NULL)) {
679 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700680 }
681 return 0; // Will throw exception by checking with Thread::Current
682}
683
684extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
685 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800686 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700687 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800688 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700689 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700690 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800691 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
692 if (LIKELY(field != NULL)) {
693 return field->GetObj(NULL);
694 }
695 return NULL; // Will throw exception by checking with Thread::Current
696}
697
698extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
699 const Method* referrer, Thread* self, Method** sp) {
700 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
701 if (LIKELY(field != NULL && obj != NULL)) {
702 return field->Get32(obj);
703 }
704 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
705 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
706 if (LIKELY(field != NULL)) {
707 if (UNLIKELY(obj == NULL)) {
708 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700709 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800710 return field->Get32(obj);
711 }
712 }
713 return 0; // Will throw exception by checking with Thread::Current
714}
715
716extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
717 const Method* referrer, Thread* self, Method** sp) {
718 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
719 if (LIKELY(field != NULL && obj != NULL)) {
720 return field->Get64(obj);
721 }
722 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
723 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
724 if (LIKELY(field != NULL)) {
725 if (UNLIKELY(obj == NULL)) {
726 ThrowNullPointerExceptionForFieldAccess(self, field, true);
727 } else {
728 return field->Get64(obj);
729 }
730 }
731 return 0; // Will throw exception by checking with Thread::Current
732}
733
734extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
735 const Method* referrer, Thread* self, Method** sp) {
736 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
737 if (LIKELY(field != NULL && obj != NULL)) {
738 return field->GetObj(obj);
739 }
740 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
741 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
742 if (LIKELY(field != NULL)) {
743 if (UNLIKELY(obj == NULL)) {
744 ThrowNullPointerExceptionForFieldAccess(self, field, true);
745 } else {
746 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700747 }
748 }
749 return NULL; // Will throw exception by checking with Thread::Current
750}
751
Ian Rogers1bddec32012-02-04 12:27:34 -0800752extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
753 const Method* referrer, Thread* self, Method** sp) {
754 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700755 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800756 field->Set32(NULL, new_value);
757 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700758 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700759 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800760 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
761 if (LIKELY(field != NULL)) {
762 field->Set32(NULL, new_value);
763 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700764 }
765 return -1; // failure
766}
767
768extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
769 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800770 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700771 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800772 field->Set64(NULL, new_value);
773 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700774 }
775 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800776 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700777 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800778 field->Set64(NULL, new_value);
779 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700780 }
781 return -1; // failure
782}
783
Ian Rogers1bddec32012-02-04 12:27:34 -0800784extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
785 const Method* referrer, Thread* self, Method** sp) {
786 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700787 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800788 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700789 field->SetObj(NULL, new_value);
790 return 0; // success
791 }
792 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700793 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800794 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
795 if (LIKELY(field != NULL)) {
796 field->SetObj(NULL, new_value);
797 return 0; // success
798 }
799 return -1; // failure
800}
801
802extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
803 const Method* referrer, Thread* self, Method** sp) {
804 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
805 if (LIKELY(field != NULL && obj != NULL)) {
806 field->Set32(obj, new_value);
807 return 0; // success
808 }
809 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
810 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
811 if (LIKELY(field != NULL)) {
812 if (UNLIKELY(obj == NULL)) {
813 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700814 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800815 field->Set32(obj, new_value);
816 return 0; // success
817 }
818 }
819 return -1; // failure
820}
821
822extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
823 Thread* self, Method** sp) {
824 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
825 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
826 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
827 if (LIKELY(field != NULL && obj != NULL)) {
828 field->Set64(obj, new_value);
829 return 0; // success
830 }
831 *sp = callee_save;
832 self->SetTopOfStack(sp, 0);
833 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
834 if (LIKELY(field != NULL)) {
835 if (UNLIKELY(obj == NULL)) {
836 ThrowNullPointerExceptionForFieldAccess(self, field, false);
837 } else {
838 field->Set64(obj, new_value);
839 return 0; // success
840 }
841 }
842 return -1; // failure
843}
844
845extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
846 const Method* referrer, Thread* self, Method** sp) {
847 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
848 if (LIKELY(field != NULL && obj != NULL)) {
849 field->SetObj(obj, new_value);
850 return 0; // success
851 }
852 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
853 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
854 if (LIKELY(field != NULL)) {
855 if (UNLIKELY(obj == NULL)) {
856 ThrowNullPointerExceptionForFieldAccess(self, field, false);
857 } else {
858 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700859 return 0; // success
860 }
861 }
862 return -1; // failure
863}
864
Shih-wei Liao2d831012011-09-28 22:06:53 -0700865// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
866// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800867// When verification/compiler hasn't been able to verify access, optionally perform an access
868// check.
869static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
870 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700871 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700872 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700873 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700874 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700875 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700876 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700877 return NULL; // Failure
878 }
879 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800880 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800881 if (UNLIKELY(!klass->IsInstantiable())) {
882 self->ThrowNewException("Ljava/lang/InstantiationError;",
883 PrettyDescriptor(klass).c_str());
884 return NULL; // Failure
885 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800886 Class* referrer = method->GetDeclaringClass();
887 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800888 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800889 return NULL; // Failure
890 }
891 }
buzbee33a129c2011-10-06 16:53:20 -0700892 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
893 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700894 return NULL; // Failure
895 }
896 return klass->AllocObject();
897}
898
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800899extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
900 Thread* self, Method** sp) {
901 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
902 return AllocObjectFromCode(type_idx, method, self, false);
903}
904
Ian Rogers28ad40d2011-10-27 15:19:26 -0700905extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
906 Thread* self, Method** sp) {
907 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800908 return AllocObjectFromCode(type_idx, method, self, true);
909}
910
911// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
912// it cannot be resolved, throw an error. If it can, use it to create an array.
913// When verification/compiler hasn't been able to verify access, optionally perform an access
914// check.
915static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
916 Thread* self, bool access_check) {
917 if (UNLIKELY(component_count < 0)) {
918 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
919 component_count);
920 return NULL; // Failure
921 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700922 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800923 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
924 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
925 if (klass == NULL) { // Error
926 DCHECK(Thread::Current()->IsExceptionPending());
927 return NULL; // Failure
928 }
929 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
930 }
931 if (access_check) {
932 Class* referrer = method->GetDeclaringClass();
933 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800934 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700935 return NULL; // Failure
936 }
937 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800938 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700939}
940
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800941extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
942 Thread* self, Method** sp) {
943 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
944 return AllocArrayFromCode(type_idx, method, component_count, self, false);
945}
946
947extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
948 int32_t component_count,
949 Thread* self, Method** sp) {
950 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
951 return AllocArrayFromCode(type_idx, method, component_count, self, true);
952}
953
954// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700955Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800956 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700957 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700958 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700959 return NULL; // Failure
960 }
961 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700962 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700963 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
964 if (klass == NULL) { // Error
965 DCHECK(Thread::Current()->IsExceptionPending());
966 return NULL; // Failure
967 }
968 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700969 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700970 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700971 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700972 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800973 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700974 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700975 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700976 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800977 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700978 }
979 return NULL; // Failure
980 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800981 if (access_check) {
982 Class* referrer = method->GetDeclaringClass();
983 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800984 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800985 return NULL; // Failure
986 }
987 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700988 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700989 return Array::Alloc(klass, component_count);
990 }
991}
992
Ian Rogersce9eca62011-10-07 17:11:03 -0700993extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
994 int32_t component_count, Thread* self, Method** sp) {
995 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800996 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700997}
998
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800999extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
1000 int32_t component_count,
1001 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001002 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001003 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001004}
1005
Ian Rogerscaab8c42011-10-12 12:11:18 -07001006// Assignable test for code, won't throw. Null and equality tests already performed
1007uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
1008 DCHECK(klass != NULL);
1009 DCHECK(ref_class != NULL);
1010 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1011}
1012
Shih-wei Liao2d831012011-09-28 22:06:53 -07001013// 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 -07001014extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001015 DCHECK(a->IsClass()) << PrettyClass(a);
1016 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001017 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001018 return 0; // Success
1019 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001020 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001021 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -07001022 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001023 PrettyDescriptor(a).c_str(),
1024 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001025 return -1; // Failure
1026 }
1027}
1028
1029// Tests whether 'element' can be assigned into an array of type 'array_class'.
1030// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001031extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
1032 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001033 DCHECK(array_class != NULL);
1034 // element can't be NULL as we catch this is screened in runtime_support
1035 Class* element_class = element->GetClass();
1036 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -07001037 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001038 return 0; // Success
1039 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001040 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001041 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -08001042 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001043 PrettyDescriptor(element_class).c_str(),
1044 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001045 return -1; // Failure
1046 }
1047}
1048
Elliott Hughesf3778f62012-01-26 14:14:35 -08001049Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
1050 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001051 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1052 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001053 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001054 CHECK(self->IsExceptionPending());
1055 return NULL; // Failure - Indicate to caller to deliver exception
1056 }
Elliott Hughesf3778f62012-01-26 14:14:35 -08001057 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001058 Class* referring_class = referrer->GetDeclaringClass();
1059 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
1060 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001061 return NULL; // Failure - Indicate to caller to deliver exception
1062 }
1063 // If we're just implementing const-class, we shouldn't call <clinit>.
1064 if (!can_run_clinit) {
1065 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -07001066 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001067 // If we are the <clinit> of this class, just return our storage.
1068 //
1069 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
1070 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001071 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001072 return klass;
1073 }
1074 if (!class_linker->EnsureInitialized(klass, true)) {
1075 CHECK(self->IsExceptionPending());
1076 return NULL; // Failure - Indicate to caller to deliver exception
1077 }
1078 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
1079 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001080}
1081
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001082extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
1083 Thread* self, Method** sp) {
1084 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001085 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001086}
1087
Ian Rogers28ad40d2011-10-27 15:19:26 -07001088extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
1089 Method** sp) {
1090 // Called when method->dex_cache_resolved_types_[] misses
1091 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001092 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001093}
1094
Ian Rogersb093c6b2011-10-31 16:19:55 -07001095extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
1096 const Method* referrer, Thread* self,
1097 Method** sp) {
1098 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
1099 // unpopulated
1100 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001101 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -07001102}
1103
buzbee48d72222012-01-11 15:19:51 -08001104// Helper function to resolve virtual method
1105extern "C" Method* artResolveMethodFromCode(Method* referrer,
1106 uint32_t method_idx,
1107 bool is_direct,
1108 Thread* self,
1109 Method** sp) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001110 /*
1111 * Slow-path handler on invoke virtual method path in which
buzbee48d72222012-01-11 15:19:51 -08001112 * base method is unresolved at compile-time. Caller will
1113 * unwind if can't resolve.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001114 */
buzbee48d72222012-01-11 15:19:51 -08001115 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1116 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1117 Method* method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
buzbee48d72222012-01-11 15:19:51 -08001118 return method;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001119}
1120
Brian Carlstromaded5f72011-10-07 17:15:04 -07001121String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1122 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1123 return class_linker->ResolveString(string_idx, referrer);
1124}
1125
1126extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1127 Thread* self, Method** sp) {
1128 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1129 return ResolveStringFromCode(referrer, string_idx);
1130}
1131
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001132extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1133 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001134 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001135 // MonitorExit may throw exception
1136 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1137}
1138
1139extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1140 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1141 DCHECK(obj != NULL); // Assumed to have been checked before entry
1142 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001143 DCHECK(thread->HoldsLock(obj));
1144 // Only possible exception is NPE and is handled before entry
1145 DCHECK(!thread->IsExceptionPending());
1146}
1147
Ian Rogers4a510d82011-10-09 14:30:24 -07001148void CheckSuspendFromCode(Thread* thread) {
1149 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001150 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1151}
1152
Ian Rogers4a510d82011-10-09 14:30:24 -07001153extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1154 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001155 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001156 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1157}
1158
1159/*
1160 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1161 * not of sufficient length.
1162 *
1163 * NOTE: When dealing with a raw dex file, the data to be copied uses
1164 * little-endian ordering. Require that oat2dex do any required swapping
1165 * so this routine can get by with a memcpy().
1166 *
1167 * Format of the data:
1168 * ushort ident = 0x0300 magic value
1169 * ushort width width of each element in the table
1170 * uint size number of elements in the table
1171 * ubyte data[size*width] table of data values (may contain a single-byte
1172 * padding at the end)
1173 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001174extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1175 Thread* self, Method** sp) {
1176 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001177 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001178 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001179 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1180 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001181 return -1; // Error
1182 }
1183 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1184 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001185 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001186 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1187 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001188 return -1; // Error
1189 }
1190 uint16_t width = table[1];
1191 uint32_t size_in_bytes = size * width;
1192 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
1193 return 0; // Success
1194}
1195
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001196// Fast path method resolution that can't throw exceptions
1197static Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001198 bool access_check, InvokeType type) {
1199 bool is_direct = type == kStatic || type == kDirect;
1200 if (UNLIKELY(this_object == NULL && !is_direct)) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001201 return NULL;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001202 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001203 Method* resolved_method =
1204 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
1205 if (UNLIKELY(resolved_method == NULL)) {
1206 return NULL;
1207 }
1208 if (access_check) {
1209 Class* methods_class = resolved_method->GetDeclaringClass();
1210 Class* referring_class = referrer->GetDeclaringClass();
1211 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1212 !referring_class->CanAccessMember(methods_class,
1213 resolved_method->GetAccessFlags()))) {
1214 // potential illegal access
1215 return NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -07001216 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001217 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001218 if (type == kInterface) { // Most common form of slow path dispatch.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001219 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001220 } else if (is_direct) {
1221 return resolved_method;
1222 } else if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001223 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1224 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001225 DCHECK(type == kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001226 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1227 }
1228}
1229
1230// Slow path method resolution
1231static Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001232 Thread* self, bool access_check, InvokeType type) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001233 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001234 bool is_direct = type == kStatic || type == kDirect;
1235 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
1236 if (UNLIKELY(resolved_method == NULL)) {
1237 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
1238 return NULL; // failure
1239 } else {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001240 if (!access_check) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001241 if (is_direct) {
1242 return resolved_method;
1243 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001244 Method* interface_method =
1245 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1246 if (UNLIKELY(interface_method == NULL)) {
1247 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1248 resolved_method,
1249 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001250 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001251 } else {
1252 return interface_method;
1253 }
1254 } else {
1255 ObjectArray<Method>* vtable;
1256 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001257 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001258 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
1259 } else {
1260 vtable = this_object->GetClass()->GetVTable();
1261 }
1262 // TODO: eliminate bounds check?
1263 return vtable->Get(vtable_index);
1264 }
1265 } else {
1266 Class* methods_class = resolved_method->GetDeclaringClass();
1267 Class* referring_class = referrer->GetDeclaringClass();
1268 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1269 !referring_class->CanAccessMember(methods_class,
1270 resolved_method->GetAccessFlags()))) {
1271 // The referring class can't access the resolved method, this may occur as a result of a
1272 // protected method being made public by implementing an interface that re-declares the
1273 // method public. Resort to the dex file to determine the correct class for the access check
1274 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
1275 methods_class = class_linker->ResolveType(dex_file,
1276 dex_file.GetMethodId(method_idx).class_idx_,
1277 referring_class);
1278 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
1279 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001280 referrer, resolved_method, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001281 return NULL; // failure
1282 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
1283 resolved_method->GetAccessFlags()))) {
1284 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
1285 return NULL; // failure
1286 }
1287 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001288 if (is_direct) {
1289 return resolved_method;
1290 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001291 Method* interface_method =
1292 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1293 if (UNLIKELY(interface_method == NULL)) {
1294 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1295 resolved_method,
1296 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001297 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001298 } else {
1299 return interface_method;
1300 }
1301 } else {
1302 ObjectArray<Method>* vtable;
1303 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001304 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001305 Class* super_class = referring_class->GetSuperClass();
1306 if (LIKELY(super_class != NULL)) {
1307 vtable = referring_class->GetSuperClass()->GetVTable();
1308 } else {
1309 vtable = NULL;
1310 }
1311 } else {
1312 vtable = this_object->GetClass()->GetVTable();
1313 }
1314 if (LIKELY(vtable != NULL &&
1315 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
1316 return vtable->GetWithoutChecks(vtable_index);
1317 } else {
1318 // Behavior to agree with that of the verifier
1319 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
1320 "attempt to invoke %s method '%s' from '%s'"
1321 " using incorrect form of method dispatch",
Ian Rogersc8b306f2012-02-17 21:34:44 -08001322 (type == kSuper ? "super class" : "virtual"),
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001323 PrettyMethod(resolved_method).c_str(),
1324 PrettyMethod(referrer).c_str());
Ian Rogersc8b306f2012-02-17 21:34:44 -08001325 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001326 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001327 }
1328 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001329 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001330}
1331
1332static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001333 Thread* self, Method** sp, bool access_check, InvokeType type){
1334 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001335 if (UNLIKELY(method == NULL)) {
1336 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001337 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
1338 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001339 return 0; // failure
1340 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001341 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001342 if (UNLIKELY(method == NULL)) {
1343 CHECK(self->IsExceptionPending());
1344 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001345 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001346 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001347 // TODO: DCHECK
1348 CHECK(!self->IsExceptionPending());
1349 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001350
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001351 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001352 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1353 uint64_t result = ((code_uint << 32) | method_uint);
1354 return result;
1355}
1356
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001357// See comments in runtime_support_asm.S
1358extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1359 Method* caller_method, Thread* self,
1360 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001361 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001362}
1363
1364extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1365 Object* this_object,
1366 Method* caller_method, Thread* self,
1367 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001368 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1369}
1370
1371
1372extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1373 Object* this_object,
1374 Method* caller_method, Thread* self,
1375 Method** sp) {
1376 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1377}
1378
1379extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1380 Object* this_object,
1381 Method* caller_method, Thread* self,
1382 Method** sp) {
1383 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001384}
1385
1386extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1387 Object* this_object,
1388 Method* caller_method, Thread* self,
1389 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001390 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001391}
1392
1393extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1394 Object* this_object,
1395 Method* caller_method, Thread* self,
1396 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001397 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001398}
1399
Ian Rogers466bb252011-10-14 03:29:56 -07001400static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1401 ScopedLocalRef<jclass> jlr_UTE_class(env,
1402 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1403 if (jlr_UTE_class.get() == NULL) {
1404 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1405 } else {
1406 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1407 "(Ljava/lang/Throwable;)V");
1408 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1409 ScopedLocalRef<jthrowable> jlr_UTE(env,
1410 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1411 jexception)));
1412 int rc = env->Throw(jlr_UTE.get());
1413 if (rc != JNI_OK) {
1414 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1415 }
1416 }
1417 CHECK(self->IsExceptionPending());
1418}
1419
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001420// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1421// which is responsible for recording callee save registers. We explicitly handlerize incoming
1422// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1423// the invocation handler which is a field within the proxy object receiver.
1424extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001425 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001426 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001427 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1428 DCHECK_EQ(*proxy_sp, proxy_method);
1429 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001430 // TODO: ARM specific
1431 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1432 // Start new JNI local reference state
1433 JNIEnvExt* env = self->GetJniEnv();
1434 ScopedJniEnvLocalRefState env_state(env);
1435 // Create local ref. copies of proxy method and the receiver
1436 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1437 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1438
Ian Rogers14b1b242011-10-11 18:54:34 -07001439 // Placing into local references incoming arguments from the caller's register arguments,
1440 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001441 MethodHelper proxy_mh(proxy_method);
1442 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001443 size_t args_in_regs = 0;
1444 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001445 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001446 if (args_in_regs > 2) {
1447 args_in_regs = 2;
1448 break;
1449 }
1450 }
1451 size_t cur_arg = 0; // current stack location to read
1452 size_t param_index = 1; // skip receiver
1453 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001454 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001455 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001456 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001457 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001458 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001459 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001460 param_index++;
1461 }
1462 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001463 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001464 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001465 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001466 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1467 jobject jobj = AddLocalReference<jobject>(env, obj);
1468 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001469 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001470 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001471 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001472 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001473 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1474 jvalue args_jobj[3];
1475 args_jobj[0].l = rcvr_jobj;
1476 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001477 // Args array, if no arguments then NULL (don't include receiver in argument count)
1478 args_jobj[2].l = NULL;
1479 ObjectArray<Object>* args = NULL;
1480 if ((num_params - 1) > 0) {
1481 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001482 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001483 CHECK(self->IsExceptionPending());
1484 return;
1485 }
1486 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1487 }
1488 // Convert proxy method into expected interface method
1489 Method* interface_method = proxy_method->FindOverriddenMethod();
1490 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001491 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001492 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001493 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001494 cur_arg = 0; // reset stack location to read to start
1495 // reset index, will index into param type array which doesn't include the receiver
1496 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001497 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001498 CHECK(param_types != NULL);
1499 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1500 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1501 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1502 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001503 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001504 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001505 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001506 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001507 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1508 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1509 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001510 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001511 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001512 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001513 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001514 if (self->IsExceptionPending()) {
1515 return;
1516 }
1517 obj = val.l;
1518 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001519 args->Set(param_index, obj);
1520 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1521 param_index++;
1522 }
1523 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001524 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1525 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001526 Class* param_type = param_types->Get(param_index);
1527 Object* obj;
1528 if (!param_type->IsPrimitive()) {
1529 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1530 } else {
1531 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001532 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001533 if (self->IsExceptionPending()) {
1534 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001535 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001536 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001537 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001538 args->Set(param_index, obj);
1539 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1540 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001541 }
1542 // Get the InvocationHandler method and the field that holds it within the Proxy object
1543 static jmethodID inv_hand_invoke_mid = NULL;
1544 static jfieldID proxy_inv_hand_fid = NULL;
1545 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001546 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001547 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001548 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001549 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1550 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1551 }
Ian Rogers466bb252011-10-14 03:29:56 -07001552 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001553 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1554 // Call InvocationHandler.invoke
1555 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1556 // Place result in stack args
1557 if (!self->IsExceptionPending()) {
1558 Object* result_ref = self->DecodeJObject(result);
1559 if (result_ref != NULL) {
1560 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001561 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001562 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1563 } else {
1564 *reinterpret_cast<jobject*>(stack_args) = NULL;
1565 }
Ian Rogers466bb252011-10-14 03:29:56 -07001566 } else {
1567 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1568 // a UndeclaredThrowableException.
1569 Throwable* exception = self->GetException();
1570 self->ClearException();
1571 if (!exception->IsCheckedException()) {
1572 self->SetException(exception);
1573 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001574 SynthesizedProxyClass* proxy_class =
1575 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1576 int throws_index = -1;
1577 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1578 for (size_t i = 0; i < num_virt_methods; i++) {
1579 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1580 throws_index = i;
1581 break;
1582 }
1583 }
1584 CHECK_NE(throws_index, -1);
1585 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001586 Class* exception_class = exception->GetClass();
1587 bool declares_exception = false;
1588 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1589 Class* declared_exception = declared_exceptions->Get(i);
1590 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1591 }
1592 if (declares_exception) {
1593 self->SetException(exception);
1594 } else {
1595 ThrowNewUndeclaredThrowableException(self, env, exception);
1596 }
1597 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001598 }
1599}
1600
jeffhaoe343b762011-12-05 16:36:44 -08001601extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001602 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001603 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1604 self->PushTraceStackFrame(trace_frame);
1605
jeffhao2692b572011-12-16 15:42:28 -08001606 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001607
jeffhao2692b572011-12-16 15:42:28 -08001608 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001609}
1610
1611extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001612 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001613 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1614 Method* method = trace_frame.method_;
1615 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001616
jeffhao2692b572011-12-16 15:42:28 -08001617 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001618
1619 return lr;
1620}
1621
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001622uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001623 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001624 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1625 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001626 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001627
jeffhao2692b572011-12-16 15:42:28 -08001628 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001629
1630 return lr;
1631}
1632
Shih-wei Liao2d831012011-09-28 22:06:53 -07001633/*
1634 * Float/double conversion requires clamping to min and max of integer form. If
1635 * target doesn't support this normally, use these.
1636 */
1637int64_t D2L(double d) {
1638 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1639 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1640 if (d >= kMaxLong)
1641 return (int64_t)0x7fffffffffffffffULL;
1642 else if (d <= kMinLong)
1643 return (int64_t)0x8000000000000000ULL;
1644 else if (d != d) // NaN case
1645 return 0;
1646 else
1647 return (int64_t)d;
1648}
1649
1650int64_t F2L(float f) {
1651 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1652 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1653 if (f >= kMaxLong)
1654 return (int64_t)0x7fffffffffffffffULL;
1655 else if (f <= kMinLong)
1656 return (int64_t)0x8000000000000000ULL;
1657 else if (f != f) // NaN case
1658 return 0;
1659 else
1660 return (int64_t)f;
1661}
1662
1663} // namespace art