blob: 8dd7eed110504c395230482f01b19569b80650a5 [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
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700391void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700392 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 Rogersad25ac52011-10-04 19:13:33 -0700417 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700418 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700419 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
420 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700421 size_t args_in_regs = 0;
422 for (size_t i = 1; i < shorty_len; i++) {
423 char c = shorty[i];
424 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
425 if (args_in_regs > 3) {
426 args_in_regs = 3;
427 break;
428 }
429 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700430 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700431 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700432 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700433 // less two as return address may span into next dex instruction
434 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800435 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
437 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700438 Instruction::Code instr_code = instr->Opcode();
439 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
440 (instr_code == Instruction::INVOKE_STATIC_RANGE);
441 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
442 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700443 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700444 is_static = type == Runtime::kStaticMethod;
445 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700446 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700447 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700448 if (!is_static) {
449 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
450 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700451 if (args_in_regs < 3) {
452 // If we thought we had fewer than 3 arguments in registers, account for the receiver
453 args_in_regs++;
454 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700455 AddLocalReference<jobject>(env, obj);
456 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700457 size_t shorty_index = 1; // skip return value
458 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
459 // R0)
460 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
461 char c = shorty[shorty_index];
462 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700463 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700464 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
465 AddLocalReference<jobject>(env, obj);
466 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700467 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
468 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700469 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700470 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700471 while (shorty_index < shorty_len) {
472 char c = shorty[shorty_index];
473 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700474 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700475 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700476 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700477 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700478 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700479 }
480 // Resolve method filling in dex cache
481 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700482 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800483 if (LIKELY(called->IsDirect())) {
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800484 // Ensure that the called method's class is initialized
485 Class* called_class = called->GetDeclaringClass();
486 linker->EnsureInitialized(called_class, true);
487 if (LIKELY(called_class->IsInitialized())) {
488 // Update CodeAndDirectMethod table and avoid the trampoline when we know the called class
489 // is initialized (see test 084-class-init SlowInit)
490 Method* caller = *caller_sp;
491 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
492 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
493 // We got this far, ensure that the declaring class is initialized
494 linker->EnsureInitialized(called->GetDeclaringClass(), true);
495 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800496 } else {
497 // Direct method has been made virtual
498 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
499 "Expected direct method but found virtual: %s",
500 PrettyMethod(called, true).c_str());
501 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700502 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700503 void* code;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700504 if (UNLIKELY(thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700505 // Something went wrong in ResolveMethod or EnsureInitialized,
506 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700507 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700508 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700509 thread->ClearException();
510 } else {
511 // Expect class to at least be initializing
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800512 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogersad25ac52011-10-04 19:13:33 -0700513 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700514 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700515 code = const_cast<void*>(called->GetCode());
516 }
517 return code;
518}
519
Ian Rogers60db5ab2012-02-20 17:02:00 -0800520static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
521 intptr_t value = *arg_ptr;
522 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
523 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
524 CHECK(Heap::IsHeapAddress(value_as_work_around_rep));
525 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
526}
527
528extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
529 DCHECK(Thread::Current() == self);
530 // TODO: this code is specific to ARM
531 // On entry the stack pointed by sp is:
532 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
533 // | LR |
534 // | R3 | arg2
535 // | R2 | arg1
536 // | R1 | jclass/jobject
537 // | R0 | JNIEnv
538 // | unused |
539 // | unused |
540 // | unused | <- sp
541 Method* jni_method = self->GetTopOfStack().GetMethod();
542 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
543 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
544 // Fix up this/jclass argument
545 WorkAroundJniBugsForJobject(arg_ptr);
546 arg_ptr++;
547 // Fix up jobject arguments
548 MethodHelper mh(jni_method);
549 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800550 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800551 char shorty_char = mh.GetShorty()[i];
552 if (shorty_char == 'L') {
553 WorkAroundJniBugsForJobject(arg_ptr);
554 }
555 if (shorty_char == 'J' || shorty_char == 'D') {
556 if (reg_num == 2) {
557 arg_ptr = sp + 8; // skip to out arguments
558 reg_num = -1;
559 } else if (reg_num == 3) {
560 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
561 reg_num = -1;
562 } else {
563 DCHECK(reg_num == -1);
564 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
565 arg_ptr += 3; // unaligned, pad and move through stack arguments
566 } else {
567 arg_ptr += 2; // aligned, move through stack arguments
568 }
569 }
570 } else {
571 if (reg_num == 2) {
572 arg_ptr++; // move through register arguments
573 reg_num++;
574 } else if (reg_num == 3) {
575 arg_ptr = sp + 8; // skip to outgoing stack arguments
576 reg_num = -1;
577 } else {
578 DCHECK(reg_num == -1);
579 arg_ptr++; // move through stack arguments
580 }
581 }
582 }
583 // Load expected destination, see Method::RegisterNative
584 return reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
585}
586
587
Ian Rogerscaab8c42011-10-12 12:11:18 -0700588// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800589static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
jeffhao8cd6dda2012-02-22 10:15:34 -0800590 size_t expected_size, bool is_set) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800591 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700592 if (UNLIKELY(resolved_field == NULL)) {
593 return NULL;
594 }
595 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800596 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700597 if (UNLIKELY(!fields_class->IsInitializing())) {
598 return NULL;
599 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800600 Class* referring_class = referrer->GetDeclaringClass();
601 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
602 !referring_class->CanAccessMember(fields_class,
jeffhao8cd6dda2012-02-22 10:15:34 -0800603 resolved_field->GetAccessFlags()) ||
604 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800605 // illegal access
606 return NULL;
607 }
608 FieldHelper fh(resolved_field);
609 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
610 fh.FieldSize() != expected_size)) {
611 return NULL;
612 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700613 return resolved_field;
614}
615
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800616
Ian Rogerscaab8c42011-10-12 12:11:18 -0700617// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800618Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
jeffhao8cd6dda2012-02-22 10:15:34 -0800619 bool is_static, bool is_primitive, bool is_set, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700620 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700621 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800622 if (UNLIKELY(resolved_field == NULL)) {
623 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
624 return NULL; // failure
625 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700626 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800627 Class* referring_class = referrer->GetDeclaringClass();
628 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800629 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800630 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800631 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
632 resolved_field->GetAccessFlags()))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800633 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
Ian Rogers1bddec32012-02-04 12:27:34 -0800634 return NULL; // failure
jeffhao8cd6dda2012-02-22 10:15:34 -0800635 } else if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
636 ThrowNewIllegalAccessErrorFinalField(self, referrer, resolved_field);
637 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800638 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800639 FieldHelper fh(resolved_field);
640 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
641 fh.FieldSize() != expected_size)) {
642 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
643 "Attempted read of %zd-bit %s on field '%s'",
644 expected_size * (32 / sizeof(int32_t)),
645 is_primitive ? "primitive" : "non-primitive",
646 PrettyField(resolved_field, true).c_str());
647 return NULL; // failure
648 } else if (!is_static) {
649 // instance fields must be being accessed on an initialized class
Ian Rogers1bddec32012-02-04 12:27:34 -0800650 return resolved_field;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800651 } else {
652 // If the class is already initializing, we must be inside <clinit>, or
653 // we'd still be waiting for the lock.
654 if (fields_class->IsInitializing()) {
655 return resolved_field;
656 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
657 return resolved_field;
658 } else {
659 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
660 return NULL; // failure
661 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800662 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700663 }
664 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700665}
666
Ian Rogersce9eca62011-10-07 17:11:03 -0700667extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
668 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800669 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700670 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800671 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700672 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700673 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800674 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800675 if (LIKELY(field != NULL)) {
676 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700677 }
678 return 0; // Will throw exception by checking with Thread::Current
679}
680
681extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
682 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800683 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700684 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800685 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700686 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700687 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800688 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800689 if (LIKELY(field != NULL)) {
690 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700691 }
692 return 0; // Will throw exception by checking with Thread::Current
693}
694
695extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
696 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800697 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700698 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800699 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700700 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700701 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800702 field = FindFieldFromCode(field_idx, referrer, self, true, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800703 if (LIKELY(field != NULL)) {
704 return field->GetObj(NULL);
705 }
706 return NULL; // Will throw exception by checking with Thread::Current
707}
708
709extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
710 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800711 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800712 if (LIKELY(field != NULL && obj != NULL)) {
713 return field->Get32(obj);
714 }
715 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800716 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800717 if (LIKELY(field != NULL)) {
718 if (UNLIKELY(obj == NULL)) {
719 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700720 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800721 return field->Get32(obj);
722 }
723 }
724 return 0; // Will throw exception by checking with Thread::Current
725}
726
727extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
728 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800729 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800730 if (LIKELY(field != NULL && obj != NULL)) {
731 return field->Get64(obj);
732 }
733 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800734 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800735 if (LIKELY(field != NULL)) {
736 if (UNLIKELY(obj == NULL)) {
737 ThrowNullPointerExceptionForFieldAccess(self, field, true);
738 } else {
739 return field->Get64(obj);
740 }
741 }
742 return 0; // Will throw exception by checking with Thread::Current
743}
744
745extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
746 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800747 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800748 if (LIKELY(field != NULL && obj != NULL)) {
749 return field->GetObj(obj);
750 }
751 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800752 field = FindFieldFromCode(field_idx, referrer, self, false, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800753 if (LIKELY(field != NULL)) {
754 if (UNLIKELY(obj == NULL)) {
755 ThrowNullPointerExceptionForFieldAccess(self, field, true);
756 } else {
757 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700758 }
759 }
760 return NULL; // Will throw exception by checking with Thread::Current
761}
762
Ian Rogers1bddec32012-02-04 12:27:34 -0800763extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
764 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800765 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700766 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800767 field->Set32(NULL, new_value);
768 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700769 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700770 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800771 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800772 if (LIKELY(field != NULL)) {
773 field->Set32(NULL, new_value);
774 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700775 }
776 return -1; // failure
777}
778
779extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
780 uint64_t new_value, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800781 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700782 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800783 field->Set64(NULL, new_value);
784 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700785 }
786 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800787 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700788 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800789 field->Set64(NULL, new_value);
790 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700791 }
792 return -1; // failure
793}
794
Ian Rogers1bddec32012-02-04 12:27:34 -0800795extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
796 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800797 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700798 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800799 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700800 field->SetObj(NULL, new_value);
801 return 0; // success
802 }
803 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700804 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800805 field = FindFieldFromCode(field_idx, referrer, self, true, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800806 if (LIKELY(field != NULL)) {
807 field->SetObj(NULL, new_value);
808 return 0; // success
809 }
810 return -1; // failure
811}
812
813extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
814 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800815 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800816 if (LIKELY(field != NULL && obj != NULL)) {
817 field->Set32(obj, new_value);
818 return 0; // success
819 }
820 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800821 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800822 if (LIKELY(field != NULL)) {
823 if (UNLIKELY(obj == NULL)) {
824 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700825 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800826 field->Set32(obj, new_value);
827 return 0; // success
828 }
829 }
830 return -1; // failure
831}
832
833extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
834 Thread* self, Method** sp) {
835 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
836 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
jeffhao8cd6dda2012-02-22 10:15:34 -0800837 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800838 if (LIKELY(field != NULL && obj != NULL)) {
839 field->Set64(obj, new_value);
840 return 0; // success
841 }
842 *sp = callee_save;
843 self->SetTopOfStack(sp, 0);
jeffhao8cd6dda2012-02-22 10:15:34 -0800844 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800845 if (LIKELY(field != NULL)) {
846 if (UNLIKELY(obj == NULL)) {
847 ThrowNullPointerExceptionForFieldAccess(self, field, false);
848 } else {
849 field->Set64(obj, new_value);
850 return 0; // success
851 }
852 }
853 return -1; // failure
854}
855
856extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
857 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800858 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800859 if (LIKELY(field != NULL && obj != NULL)) {
860 field->SetObj(obj, new_value);
861 return 0; // success
862 }
863 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800864 field = FindFieldFromCode(field_idx, referrer, self, false, false, true, sizeof(Object*));
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->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700870 return 0; // success
871 }
872 }
873 return -1; // failure
874}
875
Shih-wei Liao2d831012011-09-28 22:06:53 -0700876// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
877// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800878// When verification/compiler hasn't been able to verify access, optionally perform an access
879// check.
880static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
881 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700882 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700883 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700884 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700885 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700886 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700887 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700888 return NULL; // Failure
889 }
890 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800891 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800892 if (UNLIKELY(!klass->IsInstantiable())) {
893 self->ThrowNewException("Ljava/lang/InstantiationError;",
894 PrettyDescriptor(klass).c_str());
895 return NULL; // Failure
896 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800897 Class* referrer = method->GetDeclaringClass();
898 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800899 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800900 return NULL; // Failure
901 }
902 }
buzbee33a129c2011-10-06 16:53:20 -0700903 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
904 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700905 return NULL; // Failure
906 }
907 return klass->AllocObject();
908}
909
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800910extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
911 Thread* self, Method** sp) {
912 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
913 return AllocObjectFromCode(type_idx, method, self, false);
914}
915
Ian Rogers28ad40d2011-10-27 15:19:26 -0700916extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
917 Thread* self, Method** sp) {
918 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800919 return AllocObjectFromCode(type_idx, method, self, true);
920}
921
922// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
923// it cannot be resolved, throw an error. If it can, use it to create an array.
924// When verification/compiler hasn't been able to verify access, optionally perform an access
925// check.
926static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
927 Thread* self, bool access_check) {
928 if (UNLIKELY(component_count < 0)) {
929 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
930 component_count);
931 return NULL; // Failure
932 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700933 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800934 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
935 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
936 if (klass == NULL) { // Error
937 DCHECK(Thread::Current()->IsExceptionPending());
938 return NULL; // Failure
939 }
940 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
941 }
942 if (access_check) {
943 Class* referrer = method->GetDeclaringClass();
944 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800945 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700946 return NULL; // Failure
947 }
948 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800949 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700950}
951
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800952extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
953 Thread* self, Method** sp) {
954 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
955 return AllocArrayFromCode(type_idx, method, component_count, self, false);
956}
957
958extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
959 int32_t component_count,
960 Thread* self, Method** sp) {
961 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
962 return AllocArrayFromCode(type_idx, method, component_count, self, true);
963}
964
965// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700966Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800967 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700968 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700969 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700970 return NULL; // Failure
971 }
972 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700973 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700974 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
975 if (klass == NULL) { // Error
976 DCHECK(Thread::Current()->IsExceptionPending());
977 return NULL; // Failure
978 }
979 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700980 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700981 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700982 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700983 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800984 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700985 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700986 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700987 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800988 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700989 }
990 return NULL; // Failure
991 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800992 if (access_check) {
993 Class* referrer = method->GetDeclaringClass();
994 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800995 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800996 return NULL; // Failure
997 }
998 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700999 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001000 return Array::Alloc(klass, component_count);
1001 }
1002}
1003
Ian Rogersce9eca62011-10-07 17:11:03 -07001004extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
1005 int32_t component_count, Thread* self, Method** sp) {
1006 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001007 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -07001008}
1009
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001010extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
1011 int32_t component_count,
1012 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001013 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001014 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001015}
1016
Ian Rogerscaab8c42011-10-12 12:11:18 -07001017// Assignable test for code, won't throw. Null and equality tests already performed
1018uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
1019 DCHECK(klass != NULL);
1020 DCHECK(ref_class != NULL);
1021 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1022}
1023
Shih-wei Liao2d831012011-09-28 22:06:53 -07001024// 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 -07001025extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001026 DCHECK(a->IsClass()) << PrettyClass(a);
1027 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001028 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001029 return 0; // Success
1030 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001031 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001032 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -07001033 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001034 PrettyDescriptor(a).c_str(),
1035 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001036 return -1; // Failure
1037 }
1038}
1039
1040// Tests whether 'element' can be assigned into an array of type 'array_class'.
1041// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001042extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
1043 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001044 DCHECK(array_class != NULL);
1045 // element can't be NULL as we catch this is screened in runtime_support
1046 Class* element_class = element->GetClass();
1047 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -07001048 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
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/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -08001053 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001054 PrettyDescriptor(element_class).c_str(),
1055 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001056 return -1; // Failure
1057 }
1058}
1059
Elliott Hughesf3778f62012-01-26 14:14:35 -08001060Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
1061 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001062 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1063 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001064 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001065 CHECK(self->IsExceptionPending());
1066 return NULL; // Failure - Indicate to caller to deliver exception
1067 }
Elliott Hughesf3778f62012-01-26 14:14:35 -08001068 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001069 Class* referring_class = referrer->GetDeclaringClass();
1070 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
1071 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001072 return NULL; // Failure - Indicate to caller to deliver exception
1073 }
1074 // If we're just implementing const-class, we shouldn't call <clinit>.
1075 if (!can_run_clinit) {
1076 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -07001077 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001078 // If we are the <clinit> of this class, just return our storage.
1079 //
1080 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
1081 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001082 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001083 return klass;
1084 }
1085 if (!class_linker->EnsureInitialized(klass, true)) {
1086 CHECK(self->IsExceptionPending());
1087 return NULL; // Failure - Indicate to caller to deliver exception
1088 }
1089 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
1090 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001091}
1092
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001093extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
1094 Thread* self, Method** sp) {
1095 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001096 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001097}
1098
Ian Rogers28ad40d2011-10-27 15:19:26 -07001099extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
1100 Method** sp) {
1101 // Called when method->dex_cache_resolved_types_[] misses
1102 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001103 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001104}
1105
Ian Rogersb093c6b2011-10-31 16:19:55 -07001106extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
1107 const Method* referrer, Thread* self,
1108 Method** sp) {
1109 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
1110 // unpopulated
1111 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001112 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -07001113}
1114
buzbee48d72222012-01-11 15:19:51 -08001115// Helper function to resolve virtual method
1116extern "C" Method* artResolveMethodFromCode(Method* referrer,
1117 uint32_t method_idx,
1118 bool is_direct,
1119 Thread* self,
1120 Method** sp) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001121 /*
1122 * Slow-path handler on invoke virtual method path in which
buzbee48d72222012-01-11 15:19:51 -08001123 * base method is unresolved at compile-time. Caller will
1124 * unwind if can't resolve.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001125 */
buzbee48d72222012-01-11 15:19:51 -08001126 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1127 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1128 Method* method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
buzbee48d72222012-01-11 15:19:51 -08001129 return method;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001130}
1131
Brian Carlstromaded5f72011-10-07 17:15:04 -07001132String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1133 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1134 return class_linker->ResolveString(string_idx, referrer);
1135}
1136
1137extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1138 Thread* self, Method** sp) {
1139 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1140 return ResolveStringFromCode(referrer, string_idx);
1141}
1142
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001143extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1144 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001145 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001146 // MonitorExit may throw exception
1147 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1148}
1149
1150extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1151 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1152 DCHECK(obj != NULL); // Assumed to have been checked before entry
1153 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001154 DCHECK(thread->HoldsLock(obj));
1155 // Only possible exception is NPE and is handled before entry
1156 DCHECK(!thread->IsExceptionPending());
1157}
1158
Ian Rogers4a510d82011-10-09 14:30:24 -07001159void CheckSuspendFromCode(Thread* thread) {
1160 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001161 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1162}
1163
Ian Rogers4a510d82011-10-09 14:30:24 -07001164extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1165 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001166 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001167 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1168}
1169
1170/*
1171 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1172 * not of sufficient length.
1173 *
1174 * NOTE: When dealing with a raw dex file, the data to be copied uses
1175 * little-endian ordering. Require that oat2dex do any required swapping
1176 * so this routine can get by with a memcpy().
1177 *
1178 * Format of the data:
1179 * ushort ident = 0x0300 magic value
1180 * ushort width width of each element in the table
1181 * uint size number of elements in the table
1182 * ubyte data[size*width] table of data values (may contain a single-byte
1183 * padding at the end)
1184 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001185extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1186 Thread* self, Method** sp) {
1187 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001188 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001189 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001190 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1191 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001192 return -1; // Error
1193 }
1194 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1195 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001196 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001197 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1198 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001199 return -1; // Error
1200 }
1201 uint16_t width = table[1];
1202 uint32_t size_in_bytes = size * width;
1203 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
1204 return 0; // Success
1205}
1206
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001207// Fast path method resolution that can't throw exceptions
1208static Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001209 bool access_check, InvokeType type) {
1210 bool is_direct = type == kStatic || type == kDirect;
1211 if (UNLIKELY(this_object == NULL && !is_direct)) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001212 return NULL;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001213 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001214 Method* resolved_method =
1215 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
1216 if (UNLIKELY(resolved_method == NULL)) {
1217 return NULL;
1218 }
1219 if (access_check) {
1220 Class* methods_class = resolved_method->GetDeclaringClass();
1221 Class* referring_class = referrer->GetDeclaringClass();
1222 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1223 !referring_class->CanAccessMember(methods_class,
1224 resolved_method->GetAccessFlags()))) {
1225 // potential illegal access
1226 return NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -07001227 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001228 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001229 if (type == kInterface) { // Most common form of slow path dispatch.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001230 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001231 } else if (is_direct) {
1232 return resolved_method;
1233 } else if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001234 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1235 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001236 DCHECK(type == kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001237 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1238 }
1239}
1240
1241// Slow path method resolution
1242static Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001243 Thread* self, bool access_check, InvokeType type) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001244 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001245 bool is_direct = type == kStatic || type == kDirect;
1246 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
1247 if (UNLIKELY(resolved_method == NULL)) {
1248 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
1249 return NULL; // failure
1250 } else {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001251 if (!access_check) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001252 if (is_direct) {
1253 return resolved_method;
1254 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001255 Method* interface_method =
1256 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1257 if (UNLIKELY(interface_method == NULL)) {
1258 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1259 resolved_method,
1260 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001261 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001262 } else {
1263 return interface_method;
1264 }
1265 } else {
1266 ObjectArray<Method>* vtable;
1267 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001268 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001269 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
1270 } else {
1271 vtable = this_object->GetClass()->GetVTable();
1272 }
1273 // TODO: eliminate bounds check?
1274 return vtable->Get(vtable_index);
1275 }
1276 } else {
1277 Class* methods_class = resolved_method->GetDeclaringClass();
1278 Class* referring_class = referrer->GetDeclaringClass();
1279 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1280 !referring_class->CanAccessMember(methods_class,
1281 resolved_method->GetAccessFlags()))) {
1282 // The referring class can't access the resolved method, this may occur as a result of a
1283 // protected method being made public by implementing an interface that re-declares the
1284 // method public. Resort to the dex file to determine the correct class for the access check
1285 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
1286 methods_class = class_linker->ResolveType(dex_file,
1287 dex_file.GetMethodId(method_idx).class_idx_,
1288 referring_class);
1289 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
1290 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001291 referrer, resolved_method, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001292 return NULL; // failure
1293 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
1294 resolved_method->GetAccessFlags()))) {
1295 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
1296 return NULL; // failure
1297 }
1298 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001299 if (is_direct) {
1300 return resolved_method;
1301 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001302 Method* interface_method =
1303 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1304 if (UNLIKELY(interface_method == NULL)) {
1305 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1306 resolved_method,
1307 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001308 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001309 } else {
1310 return interface_method;
1311 }
1312 } else {
1313 ObjectArray<Method>* vtable;
1314 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001315 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001316 Class* super_class = referring_class->GetSuperClass();
1317 if (LIKELY(super_class != NULL)) {
1318 vtable = referring_class->GetSuperClass()->GetVTable();
1319 } else {
1320 vtable = NULL;
1321 }
1322 } else {
1323 vtable = this_object->GetClass()->GetVTable();
1324 }
1325 if (LIKELY(vtable != NULL &&
1326 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
1327 return vtable->GetWithoutChecks(vtable_index);
1328 } else {
1329 // Behavior to agree with that of the verifier
1330 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
1331 "attempt to invoke %s method '%s' from '%s'"
1332 " using incorrect form of method dispatch",
Ian Rogersc8b306f2012-02-17 21:34:44 -08001333 (type == kSuper ? "super class" : "virtual"),
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001334 PrettyMethod(resolved_method).c_str(),
1335 PrettyMethod(referrer).c_str());
Ian Rogersc8b306f2012-02-17 21:34:44 -08001336 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001337 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001338 }
1339 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001340 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001341}
1342
1343static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001344 Thread* self, Method** sp, bool access_check, InvokeType type){
1345 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001346 if (UNLIKELY(method == NULL)) {
1347 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001348 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
1349 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001350 return 0; // failure
1351 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001352 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001353 if (UNLIKELY(method == NULL)) {
1354 CHECK(self->IsExceptionPending());
1355 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001356 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001357 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001358 // TODO: DCHECK
1359 CHECK(!self->IsExceptionPending());
1360 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001361
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001362 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001363 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1364 uint64_t result = ((code_uint << 32) | method_uint);
1365 return result;
1366}
1367
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001368// See comments in runtime_support_asm.S
1369extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1370 Method* caller_method, Thread* self,
1371 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001372 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001373}
1374
1375extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1376 Object* this_object,
1377 Method* caller_method, Thread* self,
1378 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001379 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1380}
1381
1382
1383extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1384 Object* this_object,
1385 Method* caller_method, Thread* self,
1386 Method** sp) {
1387 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1388}
1389
1390extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1391 Object* this_object,
1392 Method* caller_method, Thread* self,
1393 Method** sp) {
1394 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001395}
1396
1397extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1398 Object* this_object,
1399 Method* caller_method, Thread* self,
1400 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001401 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001402}
1403
1404extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1405 Object* this_object,
1406 Method* caller_method, Thread* self,
1407 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001408 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001409}
1410
Ian Rogers466bb252011-10-14 03:29:56 -07001411static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1412 ScopedLocalRef<jclass> jlr_UTE_class(env,
1413 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1414 if (jlr_UTE_class.get() == NULL) {
1415 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1416 } else {
1417 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1418 "(Ljava/lang/Throwable;)V");
1419 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1420 ScopedLocalRef<jthrowable> jlr_UTE(env,
1421 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1422 jexception)));
1423 int rc = env->Throw(jlr_UTE.get());
1424 if (rc != JNI_OK) {
1425 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1426 }
1427 }
1428 CHECK(self->IsExceptionPending());
1429}
1430
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001431// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1432// which is responsible for recording callee save registers. We explicitly handlerize incoming
1433// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1434// the invocation handler which is a field within the proxy object receiver.
1435extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001436 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001437 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001438 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1439 DCHECK_EQ(*proxy_sp, proxy_method);
1440 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001441 // TODO: ARM specific
1442 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1443 // Start new JNI local reference state
1444 JNIEnvExt* env = self->GetJniEnv();
1445 ScopedJniEnvLocalRefState env_state(env);
1446 // Create local ref. copies of proxy method and the receiver
1447 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1448 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1449
Ian Rogers14b1b242011-10-11 18:54:34 -07001450 // Placing into local references incoming arguments from the caller's register arguments,
1451 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001452 MethodHelper proxy_mh(proxy_method);
1453 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001454 size_t args_in_regs = 0;
1455 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001456 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001457 if (args_in_regs > 2) {
1458 args_in_regs = 2;
1459 break;
1460 }
1461 }
1462 size_t cur_arg = 0; // current stack location to read
1463 size_t param_index = 1; // skip receiver
1464 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001465 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001466 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001467 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001468 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001469 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001470 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001471 param_index++;
1472 }
1473 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001474 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001475 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001476 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001477 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1478 jobject jobj = AddLocalReference<jobject>(env, obj);
1479 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001480 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001481 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001482 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001483 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001484 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1485 jvalue args_jobj[3];
1486 args_jobj[0].l = rcvr_jobj;
1487 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001488 // Args array, if no arguments then NULL (don't include receiver in argument count)
1489 args_jobj[2].l = NULL;
1490 ObjectArray<Object>* args = NULL;
1491 if ((num_params - 1) > 0) {
1492 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001493 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001494 CHECK(self->IsExceptionPending());
1495 return;
1496 }
1497 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1498 }
1499 // Convert proxy method into expected interface method
1500 Method* interface_method = proxy_method->FindOverriddenMethod();
1501 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001502 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001503 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001504 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001505 cur_arg = 0; // reset stack location to read to start
1506 // reset index, will index into param type array which doesn't include the receiver
1507 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001508 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001509 CHECK(param_types != NULL);
1510 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1511 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1512 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1513 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001514 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001515 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001516 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001517 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001518 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1519 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1520 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001521 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001522 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001523 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001524 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001525 if (self->IsExceptionPending()) {
1526 return;
1527 }
1528 obj = val.l;
1529 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001530 args->Set(param_index, obj);
1531 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1532 param_index++;
1533 }
1534 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001535 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1536 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001537 Class* param_type = param_types->Get(param_index);
1538 Object* obj;
1539 if (!param_type->IsPrimitive()) {
1540 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1541 } else {
1542 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001543 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001544 if (self->IsExceptionPending()) {
1545 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001546 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001547 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001548 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001549 args->Set(param_index, obj);
1550 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1551 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001552 }
1553 // Get the InvocationHandler method and the field that holds it within the Proxy object
1554 static jmethodID inv_hand_invoke_mid = NULL;
1555 static jfieldID proxy_inv_hand_fid = NULL;
1556 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001557 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001558 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001559 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001560 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1561 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1562 }
Ian Rogers466bb252011-10-14 03:29:56 -07001563 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001564 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1565 // Call InvocationHandler.invoke
1566 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1567 // Place result in stack args
1568 if (!self->IsExceptionPending()) {
1569 Object* result_ref = self->DecodeJObject(result);
1570 if (result_ref != NULL) {
1571 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001572 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001573 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1574 } else {
1575 *reinterpret_cast<jobject*>(stack_args) = NULL;
1576 }
Ian Rogers466bb252011-10-14 03:29:56 -07001577 } else {
1578 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1579 // a UndeclaredThrowableException.
1580 Throwable* exception = self->GetException();
1581 self->ClearException();
1582 if (!exception->IsCheckedException()) {
1583 self->SetException(exception);
1584 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001585 SynthesizedProxyClass* proxy_class =
1586 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1587 int throws_index = -1;
1588 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1589 for (size_t i = 0; i < num_virt_methods; i++) {
1590 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1591 throws_index = i;
1592 break;
1593 }
1594 }
1595 CHECK_NE(throws_index, -1);
1596 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001597 Class* exception_class = exception->GetClass();
1598 bool declares_exception = false;
1599 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1600 Class* declared_exception = declared_exceptions->Get(i);
1601 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1602 }
1603 if (declares_exception) {
1604 self->SetException(exception);
1605 } else {
1606 ThrowNewUndeclaredThrowableException(self, env, exception);
1607 }
1608 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001609 }
1610}
1611
jeffhaoe343b762011-12-05 16:36:44 -08001612extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001613 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001614 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1615 self->PushTraceStackFrame(trace_frame);
1616
jeffhao2692b572011-12-16 15:42:28 -08001617 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001618
jeffhao2692b572011-12-16 15:42:28 -08001619 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001620}
1621
1622extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001623 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001624 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1625 Method* method = trace_frame.method_;
1626 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001627
jeffhao2692b572011-12-16 15:42:28 -08001628 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001629
1630 return lr;
1631}
1632
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001633uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001634 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001635 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1636 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001637 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001638
jeffhao2692b572011-12-16 15:42:28 -08001639 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001640
1641 return lr;
1642}
1643
Shih-wei Liao2d831012011-09-28 22:06:53 -07001644/*
1645 * Float/double conversion requires clamping to min and max of integer form. If
1646 * target doesn't support this normally, use these.
1647 */
1648int64_t D2L(double d) {
1649 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1650 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1651 if (d >= kMaxLong)
1652 return (int64_t)0x7fffffffffffffffULL;
1653 else if (d <= kMinLong)
1654 return (int64_t)0x8000000000000000ULL;
1655 else if (d != d) // NaN case
1656 return 0;
1657 else
1658 return (int64_t)d;
1659}
1660
1661int64_t F2L(float f) {
1662 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1663 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1664 if (f >= kMaxLong)
1665 return (int64_t)0x7fffffffffffffffULL;
1666 else if (f <= kMinLong)
1667 return (int64_t)0x8000000000000000ULL;
1668 else if (f != f) // NaN case
1669 return 0;
1670 else
1671 return (int64_t)f;
1672}
1673
1674} // namespace art