blob: 00c58b5a0b5b6ffe6bb2821249357f771a377fef [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
jeffhao8cd6dda2012-02-22 10:15:34 -080079static void ThrowNewIllegalAccessErrorFinalField(Thread* self, const Method* referrer, Field* accessed) {
80 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
81 "Final field '%s' cannot be written to by method '%s'",
82 PrettyField(accessed, false).c_str(),
83 PrettyMethod(referrer).c_str());
84}
85
Ian Rogersa32a6fd2012-02-06 20:18:44 -080086static void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed) {
87 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
88 "Method '%s' is inaccessible to class '%s'",
89 PrettyMethod(accessed).c_str(),
90 PrettyDescriptor(referrer).c_str());
91}
92
93static void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read) {
94 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
95 "Attempt to %s field '%s' on a null object reference",
96 is_read ? "read from" : "write to",
97 PrettyField(field, true).c_str());
98}
99
100static void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller,
Ian Rogersc8b306f2012-02-17 21:34:44 -0800101 uint32_t method_idx, InvokeType type) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800102 const DexFile& dex_file =
103 Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
Ian Rogersc8b306f2012-02-17 21:34:44 -0800104 std::ostringstream type_stream;
105 type_stream << type;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800106 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
107 "Attempt to invoke %s method '%s' from '%s' on a null object reference",
Ian Rogersc8b306f2012-02-17 21:34:44 -0800108 type_stream.str().c_str(),
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800109 PrettyMethod(method_idx, dex_file, true).c_str(),
110 PrettyMethod(caller).c_str());
111}
112
buzbee44b412b2012-02-04 08:50:53 -0800113/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800114 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -0800115 * the method. However, because the offset alone cannot distinguish between
116 * method entry and offset 0 within the method, we'll use an offset of -1
117 * to denote method entry.
118 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800119extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -0800120 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800121 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -0800122}
123
Shih-wei Liao2d831012011-09-28 22:06:53 -0700124// Temporary debugging hook for compiler.
125extern void DebugMe(Method* method, uint32_t info) {
126 LOG(INFO) << "DebugMe";
127 if (method != NULL) {
128 LOG(INFO) << PrettyMethod(method);
129 }
130 LOG(INFO) << "Info: " << info;
131}
132
133// Return value helper for jobject return types
134extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -0700135 if (thread->IsExceptionPending()) {
136 return NULL;
137 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700138 return thread->DecodeJObject(obj);
139}
140
Ian Rogers60db5ab2012-02-20 17:02:00 -0800141extern void* FindNativeMethod(Thread* self) {
142 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700143
Ian Rogers60db5ab2012-02-20 17:02:00 -0800144 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700145 DCHECK(method != NULL);
146
147 // Lookup symbol address for method, on failure we'll return NULL with an
148 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -0800149 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700150 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800151 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700152 return NULL;
153 } else {
154 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -0800155 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700156 return native_code;
157 }
158}
159
160// Called by generated call to throw an exception
161extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
162 /*
163 * exception may be NULL, in which case this routine should
164 * throw NPE. NOTE: this is a convenience for generated code,
165 * which previously did the null check inline and constructed
166 * and threw a NPE if NULL. This routine responsible for setting
167 * exception_ in thread and delivering the exception.
168 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700169 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700170 if (exception == NULL) {
171 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
172 } else {
173 thread->SetException(exception);
174 }
175 thread->DeliverException();
176}
177
178// Deliver an exception that's pending on thread helping set up a callee save frame on the way
179extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700180 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700181 thread->DeliverException();
182}
183
184// Called by generated call to throw a NPE exception
185extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700186 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700187 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700188 thread->DeliverException();
189}
190
191// Called by generated call to throw an arithmetic divide by zero exception
192extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700193 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700194 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
195 thread->DeliverException();
196}
197
198// Called by generated call to throw an arithmetic divide by zero exception
199extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700200 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
201 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
202 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700203 thread->DeliverException();
204}
205
206// Called by the AbstractMethodError stub (not runtime support)
207extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700208 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
209 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
210 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700211 thread->DeliverException();
212}
213
214extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700215 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800216 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800217 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800218 artTraceMethodUnwindFromCode(thread);
219 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700220 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700221 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
222 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700223 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700224 thread->ResetDefaultStackEnd(); // Return to default stack size
225 thread->DeliverException();
226}
227
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700228static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700229 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700230 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
231 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
232
233 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700234 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700235 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
236 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700237 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700238 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
239 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700240 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700241 type_idx = ref;
242 } else {
243 CHECK(false) << static_cast<int>(ref_type);
244 }
245
Ian Rogers0571d352011-11-03 19:51:38 -0700246 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700247 if (!access) {
248 return class_name;
249 }
250
251 std::string result;
252 result += "tried to access class ";
253 result += class_name;
254 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800255 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700256 return result;
257}
258
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700259static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700260 verifier::VerifyErrorRefType ref_type, bool access) {
261 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700262
263 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
264 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
265
266 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700267 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700268 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700269 if (!access) {
270 return class_name + "." + field_name;
271 }
272
273 std::string result;
274 result += "tried to access field ";
275 result += class_name + "." + field_name;
276 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800277 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700278 return result;
279}
280
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700281static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700282 verifier::VerifyErrorRefType ref_type, bool access) {
283 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700284
285 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
286 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
287
288 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700289 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700290 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700291 if (!access) {
292 return class_name + "." + method_name;
293 }
294
295 std::string result;
296 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700297 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700298 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700299 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800300 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700301 return result;
302}
303
304extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700305 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
306 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700307 frame.Next();
308 Method* method = frame.GetMethod();
309
Ian Rogersd81871c2011-10-03 13:57:23 -0700310 verifier::VerifyErrorRefType ref_type =
311 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700312
313 const char* exception_class = "Ljava/lang/VerifyError;";
314 std::string msg;
315
Ian Rogersd81871c2011-10-03 13:57:23 -0700316 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
317 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700318 exception_class = "Ljava/lang/NoClassDefFoundError;";
319 msg = ClassNameFromIndex(method, ref, ref_type, false);
320 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700321 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700322 exception_class = "Ljava/lang/NoSuchFieldError;";
323 msg = FieldNameFromIndex(method, ref, ref_type, false);
324 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700326 exception_class = "Ljava/lang/NoSuchMethodError;";
327 msg = MethodNameFromIndex(method, ref, ref_type, false);
328 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700329 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700330 exception_class = "Ljava/lang/IllegalAccessError;";
331 msg = ClassNameFromIndex(method, ref, ref_type, true);
332 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700333 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700334 exception_class = "Ljava/lang/IllegalAccessError;";
335 msg = FieldNameFromIndex(method, ref, ref_type, true);
336 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700337 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700338 exception_class = "Ljava/lang/IllegalAccessError;";
339 msg = MethodNameFromIndex(method, ref, ref_type, true);
340 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700341 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700342 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
343 msg = ClassNameFromIndex(method, ref, ref_type, false);
344 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700345 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700346 exception_class = "Ljava/lang/InstantiationError;";
347 msg = ClassNameFromIndex(method, ref, ref_type, false);
348 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700349 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700350 // Generic VerifyError; use default exception, no message.
351 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700352 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700353 CHECK(false);
354 break;
355 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700356 self->ThrowNewException(exception_class, msg.c_str());
357 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700358}
359
360extern "C" void artThrowInternalErrorFromCode(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: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700363 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700364 thread->DeliverException();
365}
366
367extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700368 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700369 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700370 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700371 thread->DeliverException();
372}
373
Elliott Hughese1410a22011-10-04 12:10:24 -0700374extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700375 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
376 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700377 frame.Next();
378 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700379 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700380 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700381 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700382}
383
384extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700385 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700386 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700387 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700388 thread->DeliverException();
389}
390
Ian Rogers19846512012-02-24 11:42:47 -0800391const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread,
392 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700393 // TODO: this code is specific to ARM
394 // On entry the stack pointed by sp is:
395 // | argN | |
396 // | ... | |
397 // | arg4 | |
398 // | arg3 spill | | Caller's frame
399 // | arg2 spill | |
400 // | arg1 spill | |
401 // | Method* | ---
402 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700403 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700404 // | R3 | arg3
405 // | R2 | arg2
406 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700407 // | R0 |
408 // | Method* | <- sp
409 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
410 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
411 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
412 uintptr_t caller_pc = regs[10];
413 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700414 // Start new JNI local reference state
415 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700416 ScopedJniEnvLocalRefState env_state(env);
Ian Rogers19846512012-02-24 11:42:47 -0800417
418 // Compute details about the called method (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700419 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers19846512012-02-24 11:42:47 -0800420 Method* caller = *caller_sp;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700421 bool is_static;
Ian Rogers19846512012-02-24 11:42:47 -0800422 uint32_t dex_method_idx;
423 const char* shorty;
424 uint32_t shorty_len;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700425 if (type == Runtime::kUnknownMethod) {
Ian Rogers19846512012-02-24 11:42:47 -0800426 DCHECK(called->IsRuntimeMethod());
Ian Rogersdf9a7822011-10-11 16:53:22 -0700427 // less two as return address may span into next dex instruction
428 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
431 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700432 Instruction::Code instr_code = instr->Opcode();
433 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
434 (instr_code == Instruction::INVOKE_STATIC_RANGE);
435 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
436 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers19846512012-02-24 11:42:47 -0800437 Instruction::DecodedInstruction dec_insn(instr);
438 dex_method_idx = dec_insn.vB_;
439 shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700440 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800441 DCHECK(!called->IsRuntimeMethod());
Ian Rogersea2a11d2011-10-11 16:48:51 -0700442 is_static = type == Runtime::kStaticMethod;
Ian Rogers19846512012-02-24 11:42:47 -0800443 dex_method_idx = called->GetDexMethodIndex();
444 MethodHelper mh(called);
445 shorty = mh.GetShorty();
446 shorty_len = mh.GetShortyLength();
447 }
448 // Discover shorty (avoid GCs)
449 size_t args_in_regs = 0;
450 for (size_t i = 1; i < shorty_len; i++) {
451 char c = shorty[i];
452 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
453 if (args_in_regs > 3) {
454 args_in_regs = 3;
455 break;
456 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700457 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700458 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700459 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700460 if (!is_static) {
461 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
462 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700463 if (args_in_regs < 3) {
464 // If we thought we had fewer than 3 arguments in registers, account for the receiver
465 args_in_regs++;
466 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700467 AddLocalReference<jobject>(env, obj);
468 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700469 size_t shorty_index = 1; // skip return value
470 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
471 // R0)
472 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
473 char c = shorty[shorty_index];
474 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700475 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700476 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
477 AddLocalReference<jobject>(env, obj);
478 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700479 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
480 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700481 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700482 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700483 while (shorty_index < shorty_len) {
484 char c = shorty[shorty_index];
485 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700486 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700487 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700488 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700489 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700490 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700491 }
492 // Resolve method filling in dex cache
Ian Rogers19846512012-02-24 11:42:47 -0800493 if (type == Runtime::kUnknownMethod) {
494 called = linker->ResolveMethod(dex_method_idx, caller, true);
495 }
496 const void* code = NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700497 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800498 if (LIKELY(called->IsDirect())) {
Ian Rogers19846512012-02-24 11:42:47 -0800499 // Ensure that the called method's class is initialized.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800500 Class* called_class = called->GetDeclaringClass();
501 linker->EnsureInitialized(called_class, true);
502 if (LIKELY(called_class->IsInitialized())) {
Ian Rogers19846512012-02-24 11:42:47 -0800503 code = called->GetCode();
504 } else if (called_class->IsInitializing()) {
505 // Class is still initializing, go to oat and grab code (trampoline must be left in place
506 // until class is initialized to stop races between threads).
507 code = linker->GetOatCodeFor(called);
508 } else {
509 DCHECK(called_class->IsErroneous());
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800510 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800511 } else {
512 // Direct method has been made virtual
513 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
514 "Expected direct method but found virtual: %s",
515 PrettyMethod(called, true).c_str());
516 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700517 }
Ian Rogers19846512012-02-24 11:42:47 -0800518 if (UNLIKELY(code == NULL)) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700519 // Something went wrong in ResolveMethod or EnsureInitialized,
520 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700521 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700522 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700523 thread->ClearException();
524 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800525 // Expect class to at least be initializing.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800526 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogers19846512012-02-24 11:42:47 -0800527 // Don't want infinite recursion.
528 DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData());
Ian Rogersad25ac52011-10-04 19:13:33 -0700529 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700530 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700531 }
532 return code;
533}
534
Ian Rogers60db5ab2012-02-20 17:02:00 -0800535static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
536 intptr_t value = *arg_ptr;
537 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
538 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
539 CHECK(Heap::IsHeapAddress(value_as_work_around_rep));
540 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
541}
542
543extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
544 DCHECK(Thread::Current() == self);
545 // TODO: this code is specific to ARM
546 // On entry the stack pointed by sp is:
547 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
548 // | LR |
549 // | R3 | arg2
550 // | R2 | arg1
551 // | R1 | jclass/jobject
552 // | R0 | JNIEnv
553 // | unused |
554 // | unused |
555 // | unused | <- sp
556 Method* jni_method = self->GetTopOfStack().GetMethod();
557 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
558 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
559 // Fix up this/jclass argument
560 WorkAroundJniBugsForJobject(arg_ptr);
561 arg_ptr++;
562 // Fix up jobject arguments
563 MethodHelper mh(jni_method);
564 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800565 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800566 char shorty_char = mh.GetShorty()[i];
567 if (shorty_char == 'L') {
568 WorkAroundJniBugsForJobject(arg_ptr);
569 }
570 if (shorty_char == 'J' || shorty_char == 'D') {
571 if (reg_num == 2) {
572 arg_ptr = sp + 8; // skip to out arguments
573 reg_num = -1;
574 } else if (reg_num == 3) {
575 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
576 reg_num = -1;
577 } else {
578 DCHECK(reg_num == -1);
579 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
580 arg_ptr += 3; // unaligned, pad and move through stack arguments
581 } else {
582 arg_ptr += 2; // aligned, move through stack arguments
583 }
584 }
585 } else {
586 if (reg_num == 2) {
587 arg_ptr++; // move through register arguments
588 reg_num++;
589 } else if (reg_num == 3) {
590 arg_ptr = sp + 8; // skip to outgoing stack arguments
591 reg_num = -1;
592 } else {
593 DCHECK(reg_num == -1);
594 arg_ptr++; // move through stack arguments
595 }
596 }
597 }
598 // Load expected destination, see Method::RegisterNative
Ian Rogers19846512012-02-24 11:42:47 -0800599 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
600 if (UNLIKELY(code == NULL)) {
601 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
602 jni_method->RegisterNative(self, code);
603 }
604 return code;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800605}
606
607
Ian Rogerscaab8c42011-10-12 12:11:18 -0700608// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800609static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
jeffhao8cd6dda2012-02-22 10:15:34 -0800610 size_t expected_size, bool is_set) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800611 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700612 if (UNLIKELY(resolved_field == NULL)) {
613 return NULL;
614 }
615 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800616 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700617 if (UNLIKELY(!fields_class->IsInitializing())) {
618 return NULL;
619 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800620 Class* referring_class = referrer->GetDeclaringClass();
621 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
622 !referring_class->CanAccessMember(fields_class,
jeffhao8cd6dda2012-02-22 10:15:34 -0800623 resolved_field->GetAccessFlags()) ||
624 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800625 // illegal access
626 return NULL;
627 }
628 FieldHelper fh(resolved_field);
629 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
630 fh.FieldSize() != expected_size)) {
631 return NULL;
632 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700633 return resolved_field;
634}
635
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800636
Ian Rogerscaab8c42011-10-12 12:11:18 -0700637// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800638Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
jeffhao8cd6dda2012-02-22 10:15:34 -0800639 bool is_static, bool is_primitive, bool is_set, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700640 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700641 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800642 if (UNLIKELY(resolved_field == NULL)) {
643 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
644 return NULL; // failure
645 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700646 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800647 Class* referring_class = referrer->GetDeclaringClass();
648 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800649 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800650 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800651 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
652 resolved_field->GetAccessFlags()))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800653 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
Ian Rogers1bddec32012-02-04 12:27:34 -0800654 return NULL; // failure
jeffhao8cd6dda2012-02-22 10:15:34 -0800655 } else if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
656 ThrowNewIllegalAccessErrorFinalField(self, referrer, resolved_field);
657 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800658 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800659 FieldHelper fh(resolved_field);
660 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
661 fh.FieldSize() != expected_size)) {
662 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
663 "Attempted read of %zd-bit %s on field '%s'",
664 expected_size * (32 / sizeof(int32_t)),
665 is_primitive ? "primitive" : "non-primitive",
666 PrettyField(resolved_field, true).c_str());
667 return NULL; // failure
668 } else if (!is_static) {
669 // instance fields must be being accessed on an initialized class
Ian Rogers1bddec32012-02-04 12:27:34 -0800670 return resolved_field;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800671 } else {
672 // If the class is already initializing, we must be inside <clinit>, or
673 // we'd still be waiting for the lock.
674 if (fields_class->IsInitializing()) {
675 return resolved_field;
676 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
677 return resolved_field;
678 } else {
679 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
680 return NULL; // failure
681 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800682 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700683 }
684 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700685}
686
Ian Rogersce9eca62011-10-07 17:11:03 -0700687extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
688 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800689 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700690 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800691 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700692 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700693 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800694 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800695 if (LIKELY(field != NULL)) {
696 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700697 }
698 return 0; // Will throw exception by checking with Thread::Current
699}
700
701extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
702 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800703 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700704 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800705 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700706 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700707 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800708 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800709 if (LIKELY(field != NULL)) {
710 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700711 }
712 return 0; // Will throw exception by checking with Thread::Current
713}
714
715extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
716 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800717 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700718 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800719 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700720 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700721 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800722 field = FindFieldFromCode(field_idx, referrer, self, true, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800723 if (LIKELY(field != NULL)) {
724 return field->GetObj(NULL);
725 }
726 return NULL; // Will throw exception by checking with Thread::Current
727}
728
729extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
730 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800731 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800732 if (LIKELY(field != NULL && obj != NULL)) {
733 return field->Get32(obj);
734 }
735 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800736 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800737 if (LIKELY(field != NULL)) {
738 if (UNLIKELY(obj == NULL)) {
739 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700740 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800741 return field->Get32(obj);
742 }
743 }
744 return 0; // Will throw exception by checking with Thread::Current
745}
746
747extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
748 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800749 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800750 if (LIKELY(field != NULL && obj != NULL)) {
751 return field->Get64(obj);
752 }
753 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800754 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800755 if (LIKELY(field != NULL)) {
756 if (UNLIKELY(obj == NULL)) {
757 ThrowNullPointerExceptionForFieldAccess(self, field, true);
758 } else {
759 return field->Get64(obj);
760 }
761 }
762 return 0; // Will throw exception by checking with Thread::Current
763}
764
765extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
766 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800767 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800768 if (LIKELY(field != NULL && obj != NULL)) {
769 return field->GetObj(obj);
770 }
771 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800772 field = FindFieldFromCode(field_idx, referrer, self, false, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800773 if (LIKELY(field != NULL)) {
774 if (UNLIKELY(obj == NULL)) {
775 ThrowNullPointerExceptionForFieldAccess(self, field, true);
776 } else {
777 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700778 }
779 }
780 return NULL; // Will throw exception by checking with Thread::Current
781}
782
Ian Rogers1bddec32012-02-04 12:27:34 -0800783extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
784 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800785 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700786 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800787 field->Set32(NULL, new_value);
788 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700789 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700790 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800791 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800792 if (LIKELY(field != NULL)) {
793 field->Set32(NULL, new_value);
794 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700795 }
796 return -1; // failure
797}
798
799extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
800 uint64_t new_value, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800801 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700802 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800803 field->Set64(NULL, new_value);
804 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700805 }
806 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800807 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700808 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800809 field->Set64(NULL, new_value);
810 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700811 }
812 return -1; // failure
813}
814
Ian Rogers1bddec32012-02-04 12:27:34 -0800815extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
816 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800817 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700818 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800819 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700820 field->SetObj(NULL, new_value);
821 return 0; // success
822 }
823 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700824 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800825 field = FindFieldFromCode(field_idx, referrer, self, true, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800826 if (LIKELY(field != NULL)) {
827 field->SetObj(NULL, new_value);
828 return 0; // success
829 }
830 return -1; // failure
831}
832
833extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
834 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800835 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800836 if (LIKELY(field != NULL && obj != NULL)) {
837 field->Set32(obj, new_value);
838 return 0; // success
839 }
840 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800841 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800842 if (LIKELY(field != NULL)) {
843 if (UNLIKELY(obj == NULL)) {
844 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700845 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800846 field->Set32(obj, new_value);
847 return 0; // success
848 }
849 }
850 return -1; // failure
851}
852
853extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
854 Thread* self, Method** sp) {
855 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
856 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
jeffhao8cd6dda2012-02-22 10:15:34 -0800857 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800858 if (LIKELY(field != NULL && obj != NULL)) {
859 field->Set64(obj, new_value);
860 return 0; // success
861 }
862 *sp = callee_save;
863 self->SetTopOfStack(sp, 0);
jeffhao8cd6dda2012-02-22 10:15:34 -0800864 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800865 if (LIKELY(field != NULL)) {
866 if (UNLIKELY(obj == NULL)) {
867 ThrowNullPointerExceptionForFieldAccess(self, field, false);
868 } else {
869 field->Set64(obj, new_value);
870 return 0; // success
871 }
872 }
873 return -1; // failure
874}
875
876extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
877 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800878 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800879 if (LIKELY(field != NULL && obj != NULL)) {
880 field->SetObj(obj, new_value);
881 return 0; // success
882 }
883 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800884 field = FindFieldFromCode(field_idx, referrer, self, false, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800885 if (LIKELY(field != NULL)) {
886 if (UNLIKELY(obj == NULL)) {
887 ThrowNullPointerExceptionForFieldAccess(self, field, false);
888 } else {
889 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700890 return 0; // success
891 }
892 }
893 return -1; // failure
894}
895
Shih-wei Liao2d831012011-09-28 22:06:53 -0700896// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
897// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800898// When verification/compiler hasn't been able to verify access, optionally perform an access
899// check.
900static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
901 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700902 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700903 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700904 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700905 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700906 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700907 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700908 return NULL; // Failure
909 }
910 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800911 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800912 if (UNLIKELY(!klass->IsInstantiable())) {
913 self->ThrowNewException("Ljava/lang/InstantiationError;",
914 PrettyDescriptor(klass).c_str());
915 return NULL; // Failure
916 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800917 Class* referrer = method->GetDeclaringClass();
918 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800919 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800920 return NULL; // Failure
921 }
922 }
buzbee33a129c2011-10-06 16:53:20 -0700923 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
924 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700925 return NULL; // Failure
926 }
927 return klass->AllocObject();
928}
929
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800930extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
931 Thread* self, Method** sp) {
932 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
933 return AllocObjectFromCode(type_idx, method, self, false);
934}
935
Ian Rogers28ad40d2011-10-27 15:19:26 -0700936extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
937 Thread* self, Method** sp) {
938 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800939 return AllocObjectFromCode(type_idx, method, self, true);
940}
941
942// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
943// it cannot be resolved, throw an error. If it can, use it to create an array.
944// When verification/compiler hasn't been able to verify access, optionally perform an access
945// check.
946static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
947 Thread* self, bool access_check) {
948 if (UNLIKELY(component_count < 0)) {
949 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
950 component_count);
951 return NULL; // Failure
952 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700953 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800954 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
955 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
956 if (klass == NULL) { // Error
957 DCHECK(Thread::Current()->IsExceptionPending());
958 return NULL; // Failure
959 }
960 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
961 }
962 if (access_check) {
963 Class* referrer = method->GetDeclaringClass();
964 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800965 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700966 return NULL; // Failure
967 }
968 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800969 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700970}
971
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800972extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
973 Thread* self, Method** sp) {
974 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
975 return AllocArrayFromCode(type_idx, method, component_count, self, false);
976}
977
978extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
979 int32_t component_count,
980 Thread* self, Method** sp) {
981 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
982 return AllocArrayFromCode(type_idx, method, component_count, self, true);
983}
984
985// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700986Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800987 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700988 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700989 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700990 return NULL; // Failure
991 }
992 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700993 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700994 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
995 if (klass == NULL) { // Error
996 DCHECK(Thread::Current()->IsExceptionPending());
997 return NULL; // Failure
998 }
999 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001000 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001001 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001002 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -07001003 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001004 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001005 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001006 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -07001007 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001008 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001009 }
1010 return NULL; // Failure
1011 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001012 if (access_check) {
1013 Class* referrer = method->GetDeclaringClass();
1014 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001015 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001016 return NULL; // Failure
1017 }
1018 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001019 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001020 return Array::Alloc(klass, component_count);
1021 }
1022}
1023
Ian Rogersce9eca62011-10-07 17:11:03 -07001024extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
1025 int32_t component_count, Thread* self, Method** sp) {
1026 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001027 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -07001028}
1029
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001030extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
1031 int32_t component_count,
1032 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001033 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001034 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001035}
1036
Ian Rogerscaab8c42011-10-12 12:11:18 -07001037// Assignable test for code, won't throw. Null and equality tests already performed
1038uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
1039 DCHECK(klass != NULL);
1040 DCHECK(ref_class != NULL);
1041 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1042}
1043
Shih-wei Liao2d831012011-09-28 22:06:53 -07001044// 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 -07001045extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001046 DCHECK(a->IsClass()) << PrettyClass(a);
1047 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001048 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001049 return 0; // Success
1050 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001051 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001052 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -07001053 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001054 PrettyDescriptor(a).c_str(),
1055 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001056 return -1; // Failure
1057 }
1058}
1059
1060// Tests whether 'element' can be assigned into an array of type 'array_class'.
1061// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001062extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
1063 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001064 DCHECK(array_class != NULL);
1065 // element can't be NULL as we catch this is screened in runtime_support
1066 Class* element_class = element->GetClass();
1067 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -07001068 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001069 return 0; // Success
1070 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001071 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001072 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -08001073 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001074 PrettyDescriptor(element_class).c_str(),
1075 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001076 return -1; // Failure
1077 }
1078}
1079
Elliott Hughesf3778f62012-01-26 14:14:35 -08001080Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
1081 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001082 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1083 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001084 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001085 CHECK(self->IsExceptionPending());
1086 return NULL; // Failure - Indicate to caller to deliver exception
1087 }
Elliott Hughesf3778f62012-01-26 14:14:35 -08001088 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001089 Class* referring_class = referrer->GetDeclaringClass();
1090 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
1091 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001092 return NULL; // Failure - Indicate to caller to deliver exception
1093 }
1094 // If we're just implementing const-class, we shouldn't call <clinit>.
1095 if (!can_run_clinit) {
1096 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -07001097 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001098 // If we are the <clinit> of this class, just return our storage.
1099 //
1100 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
1101 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001102 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001103 return klass;
1104 }
1105 if (!class_linker->EnsureInitialized(klass, true)) {
1106 CHECK(self->IsExceptionPending());
1107 return NULL; // Failure - Indicate to caller to deliver exception
1108 }
1109 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
1110 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001111}
1112
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001113extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
1114 Thread* self, Method** sp) {
1115 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001116 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001117}
1118
Ian Rogers28ad40d2011-10-27 15:19:26 -07001119extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
1120 Method** sp) {
1121 // Called when method->dex_cache_resolved_types_[] misses
1122 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001123 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001124}
1125
Ian Rogersb093c6b2011-10-31 16:19:55 -07001126extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
1127 const Method* referrer, Thread* self,
1128 Method** sp) {
1129 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
1130 // unpopulated
1131 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001132 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -07001133}
1134
Brian Carlstromaded5f72011-10-07 17:15:04 -07001135String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1136 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1137 return class_linker->ResolveString(string_idx, referrer);
1138}
1139
1140extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1141 Thread* self, Method** sp) {
1142 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1143 return ResolveStringFromCode(referrer, string_idx);
1144}
1145
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001146extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1147 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001148 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001149 // MonitorExit may throw exception
1150 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1151}
1152
1153extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1154 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1155 DCHECK(obj != NULL); // Assumed to have been checked before entry
1156 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001157 DCHECK(thread->HoldsLock(obj));
1158 // Only possible exception is NPE and is handled before entry
1159 DCHECK(!thread->IsExceptionPending());
1160}
1161
Ian Rogers4a510d82011-10-09 14:30:24 -07001162void CheckSuspendFromCode(Thread* thread) {
1163 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001164 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1165}
1166
Ian Rogers4a510d82011-10-09 14:30:24 -07001167extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1168 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001169 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001170 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1171}
1172
1173/*
1174 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1175 * not of sufficient length.
1176 *
1177 * NOTE: When dealing with a raw dex file, the data to be copied uses
1178 * little-endian ordering. Require that oat2dex do any required swapping
1179 * so this routine can get by with a memcpy().
1180 *
1181 * Format of the data:
1182 * ushort ident = 0x0300 magic value
1183 * ushort width width of each element in the table
1184 * uint size number of elements in the table
1185 * ubyte data[size*width] table of data values (may contain a single-byte
1186 * padding at the end)
1187 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001188extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1189 Thread* self, Method** sp) {
1190 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001191 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001192 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001193 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1194 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001195 return -1; // Error
1196 }
1197 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1198 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001199 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001200 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1201 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001202 return -1; // Error
1203 }
1204 uint16_t width = table[1];
1205 uint32_t size_in_bytes = size * width;
1206 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
1207 return 0; // Success
1208}
1209
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001210// Fast path method resolution that can't throw exceptions
1211static Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001212 bool access_check, InvokeType type) {
1213 bool is_direct = type == kStatic || type == kDirect;
1214 if (UNLIKELY(this_object == NULL && !is_direct)) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001215 return NULL;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001216 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001217 Method* resolved_method =
1218 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
1219 if (UNLIKELY(resolved_method == NULL)) {
1220 return NULL;
1221 }
1222 if (access_check) {
1223 Class* methods_class = resolved_method->GetDeclaringClass();
1224 Class* referring_class = referrer->GetDeclaringClass();
1225 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1226 !referring_class->CanAccessMember(methods_class,
1227 resolved_method->GetAccessFlags()))) {
1228 // potential illegal access
1229 return NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -07001230 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001231 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001232 if (type == kInterface) { // Most common form of slow path dispatch.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001233 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001234 } else if (is_direct) {
1235 return resolved_method;
1236 } else if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001237 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1238 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001239 DCHECK(type == kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001240 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1241 }
1242}
1243
1244// Slow path method resolution
1245static Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001246 Thread* self, bool access_check, InvokeType type) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001247 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001248 bool is_direct = type == kStatic || type == kDirect;
1249 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
1250 if (UNLIKELY(resolved_method == NULL)) {
1251 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
1252 return NULL; // failure
1253 } else {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001254 if (!access_check) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001255 if (is_direct) {
1256 return resolved_method;
1257 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001258 Method* interface_method =
1259 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1260 if (UNLIKELY(interface_method == NULL)) {
1261 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1262 resolved_method,
1263 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001264 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001265 } else {
1266 return interface_method;
1267 }
1268 } else {
1269 ObjectArray<Method>* vtable;
1270 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001271 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001272 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
1273 } else {
1274 vtable = this_object->GetClass()->GetVTable();
1275 }
1276 // TODO: eliminate bounds check?
1277 return vtable->Get(vtable_index);
1278 }
1279 } else {
1280 Class* methods_class = resolved_method->GetDeclaringClass();
1281 Class* referring_class = referrer->GetDeclaringClass();
1282 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1283 !referring_class->CanAccessMember(methods_class,
1284 resolved_method->GetAccessFlags()))) {
1285 // The referring class can't access the resolved method, this may occur as a result of a
1286 // protected method being made public by implementing an interface that re-declares the
1287 // method public. Resort to the dex file to determine the correct class for the access check
1288 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
1289 methods_class = class_linker->ResolveType(dex_file,
1290 dex_file.GetMethodId(method_idx).class_idx_,
1291 referring_class);
1292 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
1293 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001294 referrer, resolved_method, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001295 return NULL; // failure
1296 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
1297 resolved_method->GetAccessFlags()))) {
1298 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
1299 return NULL; // failure
1300 }
1301 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001302 if (is_direct) {
1303 return resolved_method;
1304 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001305 Method* interface_method =
1306 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1307 if (UNLIKELY(interface_method == NULL)) {
1308 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1309 resolved_method,
1310 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001311 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001312 } else {
1313 return interface_method;
1314 }
1315 } else {
1316 ObjectArray<Method>* vtable;
1317 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001318 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001319 Class* super_class = referring_class->GetSuperClass();
1320 if (LIKELY(super_class != NULL)) {
1321 vtable = referring_class->GetSuperClass()->GetVTable();
1322 } else {
1323 vtable = NULL;
1324 }
1325 } else {
1326 vtable = this_object->GetClass()->GetVTable();
1327 }
1328 if (LIKELY(vtable != NULL &&
1329 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
1330 return vtable->GetWithoutChecks(vtable_index);
1331 } else {
1332 // Behavior to agree with that of the verifier
1333 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
1334 "attempt to invoke %s method '%s' from '%s'"
1335 " using incorrect form of method dispatch",
Ian Rogersc8b306f2012-02-17 21:34:44 -08001336 (type == kSuper ? "super class" : "virtual"),
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001337 PrettyMethod(resolved_method).c_str(),
1338 PrettyMethod(referrer).c_str());
Ian Rogersc8b306f2012-02-17 21:34:44 -08001339 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001340 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001341 }
1342 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001343 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001344}
1345
1346static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001347 Thread* self, Method** sp, bool access_check, InvokeType type){
1348 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001349 if (UNLIKELY(method == NULL)) {
1350 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001351 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
1352 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001353 return 0; // failure
1354 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001355 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001356 if (UNLIKELY(method == NULL)) {
1357 CHECK(self->IsExceptionPending());
1358 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001359 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001360 }
Ian Rogers19846512012-02-24 11:42:47 -08001361 DCHECK(!self->IsExceptionPending());
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001362 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001363
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001364 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001365 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1366 uint64_t result = ((code_uint << 32) | method_uint);
1367 return result;
1368}
1369
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001370// See comments in runtime_support_asm.S
1371extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1372 Method* caller_method, Thread* self,
1373 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001374 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001375}
1376
1377extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1378 Object* this_object,
1379 Method* caller_method, Thread* self,
1380 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001381 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1382}
1383
1384
1385extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1386 Object* this_object,
1387 Method* caller_method, Thread* self,
1388 Method** sp) {
1389 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1390}
1391
1392extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1393 Object* this_object,
1394 Method* caller_method, Thread* self,
1395 Method** sp) {
1396 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001397}
1398
1399extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1400 Object* this_object,
1401 Method* caller_method, Thread* self,
1402 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001403 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001404}
1405
1406extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1407 Object* this_object,
1408 Method* caller_method, Thread* self,
1409 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001410 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001411}
1412
Ian Rogers466bb252011-10-14 03:29:56 -07001413static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1414 ScopedLocalRef<jclass> jlr_UTE_class(env,
1415 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1416 if (jlr_UTE_class.get() == NULL) {
1417 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1418 } else {
1419 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1420 "(Ljava/lang/Throwable;)V");
1421 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1422 ScopedLocalRef<jthrowable> jlr_UTE(env,
1423 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1424 jexception)));
1425 int rc = env->Throw(jlr_UTE.get());
1426 if (rc != JNI_OK) {
1427 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1428 }
1429 }
1430 CHECK(self->IsExceptionPending());
1431}
1432
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001433// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1434// which is responsible for recording callee save registers. We explicitly handlerize incoming
1435// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1436// the invocation handler which is a field within the proxy object receiver.
1437extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001438 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001439 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001440 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1441 DCHECK_EQ(*proxy_sp, proxy_method);
1442 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001443 // TODO: ARM specific
1444 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1445 // Start new JNI local reference state
1446 JNIEnvExt* env = self->GetJniEnv();
1447 ScopedJniEnvLocalRefState env_state(env);
1448 // Create local ref. copies of proxy method and the receiver
1449 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1450 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1451
Ian Rogers14b1b242011-10-11 18:54:34 -07001452 // Placing into local references incoming arguments from the caller's register arguments,
1453 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001454 MethodHelper proxy_mh(proxy_method);
1455 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001456 size_t args_in_regs = 0;
1457 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001458 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001459 if (args_in_regs > 2) {
1460 args_in_regs = 2;
1461 break;
1462 }
1463 }
1464 size_t cur_arg = 0; // current stack location to read
1465 size_t param_index = 1; // skip receiver
1466 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001467 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001468 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001469 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001470 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001471 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001472 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001473 param_index++;
1474 }
1475 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001476 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001477 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001478 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001479 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1480 jobject jobj = AddLocalReference<jobject>(env, obj);
1481 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001482 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001483 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001484 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001485 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001486 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1487 jvalue args_jobj[3];
1488 args_jobj[0].l = rcvr_jobj;
1489 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001490 // Args array, if no arguments then NULL (don't include receiver in argument count)
1491 args_jobj[2].l = NULL;
1492 ObjectArray<Object>* args = NULL;
1493 if ((num_params - 1) > 0) {
1494 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001495 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001496 CHECK(self->IsExceptionPending());
1497 return;
1498 }
1499 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1500 }
1501 // Convert proxy method into expected interface method
1502 Method* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers19846512012-02-24 11:42:47 -08001503 DCHECK(interface_method != NULL);
1504 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001505 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001506 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001507 cur_arg = 0; // reset stack location to read to start
1508 // reset index, will index into param type array which doesn't include the receiver
1509 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001510 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers19846512012-02-24 11:42:47 -08001511 DCHECK(param_types != NULL);
Ian Rogers14b1b242011-10-11 18:54:34 -07001512 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
Ian Rogers19846512012-02-24 11:42:47 -08001513 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001514 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1515 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001516 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001517 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001518 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001519 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001520 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1521 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1522 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001523 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001524 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001525 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001526 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001527 if (self->IsExceptionPending()) {
1528 return;
1529 }
1530 obj = val.l;
1531 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001532 args->Set(param_index, obj);
1533 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1534 param_index++;
1535 }
1536 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001537 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1538 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001539 Class* param_type = param_types->Get(param_index);
1540 Object* obj;
1541 if (!param_type->IsPrimitive()) {
1542 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1543 } else {
1544 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001545 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001546 if (self->IsExceptionPending()) {
1547 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001548 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001549 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001550 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001551 args->Set(param_index, obj);
1552 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1553 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001554 }
1555 // Get the InvocationHandler method and the field that holds it within the Proxy object
1556 static jmethodID inv_hand_invoke_mid = NULL;
1557 static jfieldID proxy_inv_hand_fid = NULL;
1558 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001559 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001560 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001561 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001562 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1563 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1564 }
Ian Rogers466bb252011-10-14 03:29:56 -07001565 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001566 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1567 // Call InvocationHandler.invoke
1568 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1569 // Place result in stack args
1570 if (!self->IsExceptionPending()) {
1571 Object* result_ref = self->DecodeJObject(result);
1572 if (result_ref != NULL) {
1573 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001574 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001575 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1576 } else {
1577 *reinterpret_cast<jobject*>(stack_args) = NULL;
1578 }
Ian Rogers466bb252011-10-14 03:29:56 -07001579 } else {
1580 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1581 // a UndeclaredThrowableException.
1582 Throwable* exception = self->GetException();
1583 self->ClearException();
1584 if (!exception->IsCheckedException()) {
1585 self->SetException(exception);
1586 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001587 SynthesizedProxyClass* proxy_class =
1588 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1589 int throws_index = -1;
1590 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1591 for (size_t i = 0; i < num_virt_methods; i++) {
1592 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1593 throws_index = i;
1594 break;
1595 }
1596 }
1597 CHECK_NE(throws_index, -1);
1598 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001599 Class* exception_class = exception->GetClass();
1600 bool declares_exception = false;
1601 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1602 Class* declared_exception = declared_exceptions->Get(i);
1603 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1604 }
1605 if (declares_exception) {
1606 self->SetException(exception);
1607 } else {
1608 ThrowNewUndeclaredThrowableException(self, env, exception);
1609 }
1610 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001611 }
1612}
1613
jeffhaoe343b762011-12-05 16:36:44 -08001614extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001615 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001616 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1617 self->PushTraceStackFrame(trace_frame);
1618
jeffhao2692b572011-12-16 15:42:28 -08001619 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001620
jeffhao2692b572011-12-16 15:42:28 -08001621 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001622}
1623
1624extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001625 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001626 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1627 Method* method = trace_frame.method_;
1628 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001629
jeffhao2692b572011-12-16 15:42:28 -08001630 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001631
1632 return lr;
1633}
1634
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001635uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001636 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001637 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1638 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001639 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001640
jeffhao2692b572011-12-16 15:42:28 -08001641 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001642
1643 return lr;
1644}
1645
Shih-wei Liao2d831012011-09-28 22:06:53 -07001646/*
1647 * Float/double conversion requires clamping to min and max of integer form. If
1648 * target doesn't support this normally, use these.
1649 */
1650int64_t D2L(double d) {
1651 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1652 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1653 if (d >= kMaxLong)
1654 return (int64_t)0x7fffffffffffffffULL;
1655 else if (d <= kMinLong)
1656 return (int64_t)0x8000000000000000ULL;
1657 else if (d != d) // NaN case
1658 return 0;
1659 else
1660 return (int64_t)d;
1661}
1662
1663int64_t F2L(float f) {
1664 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1665 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1666 if (f >= kMaxLong)
1667 return (int64_t)0x7fffffffffffffffULL;
1668 else if (f <= kMinLong)
1669 return (int64_t)0x8000000000000000ULL;
1670 else if (f != f) // NaN case
1671 return 0;
1672 else
1673 return (int64_t)f;
1674}
1675
1676} // namespace art