blob: de07c0c3bbfb257e0883e3536a19449d472d7521 [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
Bill Buzbee11f9d212012-03-03 20:03:18 -080031extern "C" int art_cmpl_float(float a, float b) {
32 if (a == b) {
33 return 0;
34 } else if (a < b) {
35 return -1;
36 } else if (a > b) {
37 return 1;
38 }
39 return -1;
40}
41
42extern "C" int art_cmpg_float(float a, float b) {
43 if (a == b) {
44 return 0;
45 } else if (a < b) {
46 return -1;
47 } else if (a > b) {
48 return 1;
49 }
50 return 1;
51}
52
53extern "C" int art_cmpl_double(double a, double b) {
54 if (a == b) {
55 return 0;
56 } else if (a < b) {
57 return -1;
58 } else if (a > b) {
59 return 1;
60 }
61 return -1;
62}
63
64extern "C" int art_cmpg_double(double a, double b) {
65 if (a == b) {
66 return 0;
67 } else if (a < b) {
68 return -1;
69 } else if (a > b) {
70 return 1;
71 }
72 return 1;
73}
74
Ian Rogers4f0d07c2011-10-06 23:38:47 -070075// Place a special frame at the TOS that will save the callee saves for the given type
76static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070077 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070078 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
79 self->SetTopOfStack(sp, 0);
80}
81
Ian Rogersa32a6fd2012-02-06 20:18:44 -080082static void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed) {
83 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
84 "illegal class access: '%s' -> '%s'",
85 PrettyDescriptor(referrer).c_str(),
86 PrettyDescriptor(accessed).c_str());
87}
88
89static void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer,
90 Class* accessed, const Method* caller,
91 const Method* called,
Ian Rogersc8b306f2012-02-17 21:34:44 -080092 InvokeType type) {
93 std::ostringstream type_stream;
94 type_stream << type;
Ian Rogersa32a6fd2012-02-06 20:18:44 -080095 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
96 "illegal class access ('%s' -> '%s')"
97 "in attempt to invoke %s method '%s' from '%s'",
98 PrettyDescriptor(referrer).c_str(),
99 PrettyDescriptor(accessed).c_str(),
Ian Rogersc8b306f2012-02-17 21:34:44 -0800100 type_stream.str().c_str(),
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800101 PrettyMethod(called).c_str(),
102 PrettyMethod(caller).c_str());
103}
104
105static void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
106 const Method* referrer,
107 const Method* interface_method,
108 Object* this_object) {
109 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
110 "class '%s' does not implement interface '%s' in call to '%s' from '%s'",
111 PrettyDescriptor(this_object->GetClass()).c_str(),
112 PrettyDescriptor(interface_method->GetDeclaringClass()).c_str(),
113 PrettyMethod(interface_method).c_str(), PrettyMethod(referrer).c_str());
114}
115
116static void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed) {
117 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
118 "Field '%s' is inaccessible to class '%s'",
119 PrettyField(accessed, false).c_str(),
120 PrettyDescriptor(referrer).c_str());
121}
122
jeffhao8cd6dda2012-02-22 10:15:34 -0800123static void ThrowNewIllegalAccessErrorFinalField(Thread* self, const Method* referrer, Field* accessed) {
124 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
125 "Final field '%s' cannot be written to by method '%s'",
126 PrettyField(accessed, false).c_str(),
127 PrettyMethod(referrer).c_str());
128}
129
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800130static void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed) {
131 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
132 "Method '%s' is inaccessible to class '%s'",
133 PrettyMethod(accessed).c_str(),
134 PrettyDescriptor(referrer).c_str());
135}
136
137static void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read) {
138 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
139 "Attempt to %s field '%s' on a null object reference",
140 is_read ? "read from" : "write to",
141 PrettyField(field, true).c_str());
142}
143
144static void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller,
Ian Rogersc8b306f2012-02-17 21:34:44 -0800145 uint32_t method_idx, InvokeType type) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800146 const DexFile& dex_file =
147 Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
Ian Rogersc8b306f2012-02-17 21:34:44 -0800148 std::ostringstream type_stream;
149 type_stream << type;
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800150 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
151 "Attempt to invoke %s method '%s' from '%s' on a null object reference",
Ian Rogersc8b306f2012-02-17 21:34:44 -0800152 type_stream.str().c_str(),
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800153 PrettyMethod(method_idx, dex_file, true).c_str(),
154 PrettyMethod(caller).c_str());
155}
156
buzbee44b412b2012-02-04 08:50:53 -0800157/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800158 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -0800159 * the method. However, because the offset alone cannot distinguish between
160 * method entry and offset 0 within the method, we'll use an offset of -1
161 * to denote method entry.
162 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800163extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -0800164 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800165 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -0800166}
167
Shih-wei Liao2d831012011-09-28 22:06:53 -0700168// Temporary debugging hook for compiler.
169extern void DebugMe(Method* method, uint32_t info) {
170 LOG(INFO) << "DebugMe";
171 if (method != NULL) {
172 LOG(INFO) << PrettyMethod(method);
173 }
174 LOG(INFO) << "Info: " << info;
175}
176
177// Return value helper for jobject return types
178extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -0700179 if (thread->IsExceptionPending()) {
180 return NULL;
181 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700182 return thread->DecodeJObject(obj);
183}
184
Ian Rogers60db5ab2012-02-20 17:02:00 -0800185extern void* FindNativeMethod(Thread* self) {
186 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700187
Ian Rogers60db5ab2012-02-20 17:02:00 -0800188 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700189 DCHECK(method != NULL);
190
191 // Lookup symbol address for method, on failure we'll return NULL with an
192 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -0800193 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700194 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800195 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700196 return NULL;
197 } else {
198 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -0800199 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700200 return native_code;
201 }
202}
203
204// Called by generated call to throw an exception
205extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
206 /*
207 * exception may be NULL, in which case this routine should
208 * throw NPE. NOTE: this is a convenience for generated code,
209 * which previously did the null check inline and constructed
210 * and threw a NPE if NULL. This routine responsible for setting
211 * exception_ in thread and delivering the exception.
212 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700213 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700214 if (exception == NULL) {
215 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
216 } else {
217 thread->SetException(exception);
218 }
219 thread->DeliverException();
220}
221
222// Deliver an exception that's pending on thread helping set up a callee save frame on the way
223extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700224 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700225 thread->DeliverException();
226}
227
228// Called by generated call to throw a NPE exception
229extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700230 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700231 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700232 thread->DeliverException();
233}
234
235// Called by generated call to throw an arithmetic divide by zero exception
236extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700237 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700238 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
239 thread->DeliverException();
240}
241
242// Called by generated call to throw an arithmetic divide by zero exception
243extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700244 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
245 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
246 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700247 thread->DeliverException();
248}
249
250// Called by the AbstractMethodError stub (not runtime support)
251extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700252 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
253 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
254 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700255 thread->DeliverException();
256}
257
258extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700259 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800260 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800261 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800262 artTraceMethodUnwindFromCode(thread);
263 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700264 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700265 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
266 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700267 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700268 thread->ResetDefaultStackEnd(); // Return to default stack size
269 thread->DeliverException();
270}
271
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700272static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700273 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700274 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
275 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
276
277 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700278 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700279 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
280 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700281 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700282 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
283 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700284 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700285 type_idx = ref;
286 } else {
287 CHECK(false) << static_cast<int>(ref_type);
288 }
289
Ian Rogers0571d352011-11-03 19:51:38 -0700290 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700291 if (!access) {
292 return class_name;
293 }
294
295 std::string result;
296 result += "tried to access class ";
297 result += class_name;
298 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800299 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700300 return result;
301}
302
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700303static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700304 verifier::VerifyErrorRefType ref_type, bool access) {
305 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700306
307 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
308 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
309
310 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700311 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700312 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700313 if (!access) {
314 return class_name + "." + field_name;
315 }
316
317 std::string result;
318 result += "tried to access field ";
319 result += class_name + "." + field_name;
320 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800321 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700322 return result;
323}
324
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700325static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700326 verifier::VerifyErrorRefType ref_type, bool access) {
327 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700328
329 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
330 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
331
332 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700333 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700334 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700335 if (!access) {
336 return class_name + "." + method_name;
337 }
338
339 std::string result;
340 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700341 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700342 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700343 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800344 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700345 return result;
346}
347
348extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700349 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
350 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700351 frame.Next();
352 Method* method = frame.GetMethod();
353
Ian Rogersd81871c2011-10-03 13:57:23 -0700354 verifier::VerifyErrorRefType ref_type =
355 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700356
357 const char* exception_class = "Ljava/lang/VerifyError;";
358 std::string msg;
359
Ian Rogersd81871c2011-10-03 13:57:23 -0700360 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
361 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700362 exception_class = "Ljava/lang/NoClassDefFoundError;";
363 msg = ClassNameFromIndex(method, ref, ref_type, false);
364 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700365 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700366 exception_class = "Ljava/lang/NoSuchFieldError;";
367 msg = FieldNameFromIndex(method, ref, ref_type, false);
368 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700369 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700370 exception_class = "Ljava/lang/NoSuchMethodError;";
371 msg = MethodNameFromIndex(method, ref, ref_type, false);
372 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700373 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700374 exception_class = "Ljava/lang/IllegalAccessError;";
375 msg = ClassNameFromIndex(method, ref, ref_type, true);
376 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700378 exception_class = "Ljava/lang/IllegalAccessError;";
379 msg = FieldNameFromIndex(method, ref, ref_type, true);
380 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700381 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700382 exception_class = "Ljava/lang/IllegalAccessError;";
383 msg = MethodNameFromIndex(method, ref, ref_type, true);
384 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700385 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700386 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
387 msg = ClassNameFromIndex(method, ref, ref_type, false);
388 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700389 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700390 exception_class = "Ljava/lang/InstantiationError;";
391 msg = ClassNameFromIndex(method, ref, ref_type, false);
392 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700393 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700394 // Generic VerifyError; use default exception, no message.
395 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700396 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700397 CHECK(false);
398 break;
399 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700400 self->ThrowNewException(exception_class, msg.c_str());
401 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700402}
403
404extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700405 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700406 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700407 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700408 thread->DeliverException();
409}
410
411extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700412 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700413 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700414 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700415 thread->DeliverException();
416}
417
Elliott Hughese1410a22011-10-04 12:10:24 -0700418extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700419 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
420 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700421 frame.Next();
422 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700423 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700424 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700425 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700426}
427
428extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700429 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700430 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700431 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700432 thread->DeliverException();
433}
434
Ian Rogers19846512012-02-24 11:42:47 -0800435const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread,
436 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700437 // TODO: this code is specific to ARM
438 // On entry the stack pointed by sp is:
439 // | argN | |
440 // | ... | |
441 // | arg4 | |
442 // | arg3 spill | | Caller's frame
443 // | arg2 spill | |
444 // | arg1 spill | |
445 // | Method* | ---
446 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700447 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700448 // | R3 | arg3
449 // | R2 | arg2
450 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700451 // | R0 |
452 // | Method* | <- sp
453 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
454 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
455 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
456 uintptr_t caller_pc = regs[10];
457 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700458 // Start new JNI local reference state
459 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700460 ScopedJniEnvLocalRefState env_state(env);
Ian Rogers19846512012-02-24 11:42:47 -0800461
462 // Compute details about the called method (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700463 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers19846512012-02-24 11:42:47 -0800464 Method* caller = *caller_sp;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700465 bool is_static;
Ian Rogers19846512012-02-24 11:42:47 -0800466 uint32_t dex_method_idx;
467 const char* shorty;
468 uint32_t shorty_len;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700469 if (type == Runtime::kUnknownMethod) {
Ian Rogers19846512012-02-24 11:42:47 -0800470 DCHECK(called->IsRuntimeMethod());
Ian Rogersdf9a7822011-10-11 16:53:22 -0700471 // less two as return address may span into next dex instruction
472 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800473 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700474 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
475 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700476 Instruction::Code instr_code = instr->Opcode();
477 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
478 (instr_code == Instruction::INVOKE_STATIC_RANGE);
479 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
480 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers19846512012-02-24 11:42:47 -0800481 Instruction::DecodedInstruction dec_insn(instr);
482 dex_method_idx = dec_insn.vB_;
483 shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700484 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800485 DCHECK(!called->IsRuntimeMethod());
Ian Rogersea2a11d2011-10-11 16:48:51 -0700486 is_static = type == Runtime::kStaticMethod;
Ian Rogers19846512012-02-24 11:42:47 -0800487 dex_method_idx = called->GetDexMethodIndex();
488 MethodHelper mh(called);
489 shorty = mh.GetShorty();
490 shorty_len = mh.GetShortyLength();
491 }
492 // Discover shorty (avoid GCs)
493 size_t args_in_regs = 0;
494 for (size_t i = 1; i < shorty_len; i++) {
495 char c = shorty[i];
496 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
497 if (args_in_regs > 3) {
498 args_in_regs = 3;
499 break;
500 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700501 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700502 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700503 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700504 if (!is_static) {
505 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
506 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700507 if (args_in_regs < 3) {
508 // If we thought we had fewer than 3 arguments in registers, account for the receiver
509 args_in_regs++;
510 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700511 AddLocalReference<jobject>(env, obj);
512 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700513 size_t shorty_index = 1; // skip return value
514 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
515 // R0)
516 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
517 char c = shorty[shorty_index];
518 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700519 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700520 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
521 AddLocalReference<jobject>(env, obj);
522 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700523 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
524 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700525 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700526 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700527 while (shorty_index < shorty_len) {
528 char c = shorty[shorty_index];
529 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700530 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700531 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700532 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700533 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700534 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700535 }
536 // Resolve method filling in dex cache
Ian Rogers19846512012-02-24 11:42:47 -0800537 if (type == Runtime::kUnknownMethod) {
538 called = linker->ResolveMethod(dex_method_idx, caller, true);
539 }
540 const void* code = NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700541 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800542 if (LIKELY(called->IsDirect())) {
Ian Rogers19846512012-02-24 11:42:47 -0800543 // Ensure that the called method's class is initialized.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800544 Class* called_class = called->GetDeclaringClass();
545 linker->EnsureInitialized(called_class, true);
546 if (LIKELY(called_class->IsInitialized())) {
Ian Rogers19846512012-02-24 11:42:47 -0800547 code = called->GetCode();
548 } else if (called_class->IsInitializing()) {
549 // Class is still initializing, go to oat and grab code (trampoline must be left in place
550 // until class is initialized to stop races between threads).
551 code = linker->GetOatCodeFor(called);
552 } else {
553 DCHECK(called_class->IsErroneous());
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800554 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800555 } else {
556 // Direct method has been made virtual
557 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
558 "Expected direct method but found virtual: %s",
559 PrettyMethod(called, true).c_str());
560 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700561 }
Ian Rogers19846512012-02-24 11:42:47 -0800562 if (UNLIKELY(code == NULL)) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700563 // Something went wrong in ResolveMethod or EnsureInitialized,
564 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700565 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700566 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700567 thread->ClearException();
568 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800569 // Expect class to at least be initializing.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800570 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogers19846512012-02-24 11:42:47 -0800571 // Don't want infinite recursion.
572 DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData());
Ian Rogersad25ac52011-10-04 19:13:33 -0700573 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700574 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700575 }
576 return code;
577}
578
Ian Rogers60db5ab2012-02-20 17:02:00 -0800579static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
580 intptr_t value = *arg_ptr;
581 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
582 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
583 CHECK(Heap::IsHeapAddress(value_as_work_around_rep));
584 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
585}
586
587extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
588 DCHECK(Thread::Current() == self);
589 // TODO: this code is specific to ARM
590 // On entry the stack pointed by sp is:
591 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
592 // | LR |
593 // | R3 | arg2
594 // | R2 | arg1
595 // | R1 | jclass/jobject
596 // | R0 | JNIEnv
597 // | unused |
598 // | unused |
599 // | unused | <- sp
600 Method* jni_method = self->GetTopOfStack().GetMethod();
601 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
602 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
603 // Fix up this/jclass argument
604 WorkAroundJniBugsForJobject(arg_ptr);
605 arg_ptr++;
606 // Fix up jobject arguments
607 MethodHelper mh(jni_method);
608 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800609 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800610 char shorty_char = mh.GetShorty()[i];
611 if (shorty_char == 'L') {
612 WorkAroundJniBugsForJobject(arg_ptr);
613 }
614 if (shorty_char == 'J' || shorty_char == 'D') {
615 if (reg_num == 2) {
616 arg_ptr = sp + 8; // skip to out arguments
617 reg_num = -1;
618 } else if (reg_num == 3) {
619 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
620 reg_num = -1;
621 } else {
622 DCHECK(reg_num == -1);
623 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
624 arg_ptr += 3; // unaligned, pad and move through stack arguments
625 } else {
626 arg_ptr += 2; // aligned, move through stack arguments
627 }
628 }
629 } else {
630 if (reg_num == 2) {
631 arg_ptr++; // move through register arguments
632 reg_num++;
633 } else if (reg_num == 3) {
634 arg_ptr = sp + 8; // skip to outgoing stack arguments
635 reg_num = -1;
636 } else {
637 DCHECK(reg_num == -1);
638 arg_ptr++; // move through stack arguments
639 }
640 }
641 }
642 // Load expected destination, see Method::RegisterNative
Ian Rogers19846512012-02-24 11:42:47 -0800643 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
644 if (UNLIKELY(code == NULL)) {
645 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
646 jni_method->RegisterNative(self, code);
647 }
648 return code;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800649}
650
651
Ian Rogerscaab8c42011-10-12 12:11:18 -0700652// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800653static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
jeffhao8cd6dda2012-02-22 10:15:34 -0800654 size_t expected_size, bool is_set) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800655 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700656 if (UNLIKELY(resolved_field == NULL)) {
657 return NULL;
658 }
659 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800660 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700661 if (UNLIKELY(!fields_class->IsInitializing())) {
662 return NULL;
663 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800664 Class* referring_class = referrer->GetDeclaringClass();
665 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
666 !referring_class->CanAccessMember(fields_class,
jeffhao8cd6dda2012-02-22 10:15:34 -0800667 resolved_field->GetAccessFlags()) ||
668 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800669 // illegal access
670 return NULL;
671 }
672 FieldHelper fh(resolved_field);
673 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
674 fh.FieldSize() != expected_size)) {
675 return NULL;
676 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700677 return resolved_field;
678}
679
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800680
Ian Rogerscaab8c42011-10-12 12:11:18 -0700681// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800682Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
jeffhao8cd6dda2012-02-22 10:15:34 -0800683 bool is_static, bool is_primitive, bool is_set, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700684 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700685 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800686 if (UNLIKELY(resolved_field == NULL)) {
687 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
688 return NULL; // failure
689 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700690 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800691 Class* referring_class = referrer->GetDeclaringClass();
692 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800693 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
Ian Rogersc8b306f2012-02-17 21:34:44 -0800694 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800695 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
696 resolved_field->GetAccessFlags()))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800697 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
Ian Rogers1bddec32012-02-04 12:27:34 -0800698 return NULL; // failure
jeffhao8cd6dda2012-02-22 10:15:34 -0800699 } else if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
700 ThrowNewIllegalAccessErrorFinalField(self, referrer, resolved_field);
701 return NULL; // failure
Ian Rogers1bddec32012-02-04 12:27:34 -0800702 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -0800703 FieldHelper fh(resolved_field);
704 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
705 fh.FieldSize() != expected_size)) {
706 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
707 "Attempted read of %zd-bit %s on field '%s'",
708 expected_size * (32 / sizeof(int32_t)),
709 is_primitive ? "primitive" : "non-primitive",
710 PrettyField(resolved_field, true).c_str());
711 return NULL; // failure
712 } else if (!is_static) {
713 // instance fields must be being accessed on an initialized class
Ian Rogers1bddec32012-02-04 12:27:34 -0800714 return resolved_field;
Ian Rogersc8b306f2012-02-17 21:34:44 -0800715 } else {
716 // If the class is already initializing, we must be inside <clinit>, or
717 // we'd still be waiting for the lock.
718 if (fields_class->IsInitializing()) {
719 return resolved_field;
720 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
721 return resolved_field;
722 } else {
723 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
724 return NULL; // failure
725 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800726 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700727 }
728 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700729}
730
Ian Rogersce9eca62011-10-07 17:11:03 -0700731extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
732 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800733 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700734 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800735 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700736 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700737 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800738 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800739 if (LIKELY(field != NULL)) {
740 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700741 }
742 return 0; // Will throw exception by checking with Thread::Current
743}
744
745extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
746 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800747 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700748 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800749 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700750 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700751 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800752 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800753 if (LIKELY(field != NULL)) {
754 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700755 }
756 return 0; // Will throw exception by checking with Thread::Current
757}
758
759extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
760 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800761 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700762 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800763 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700764 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700765 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800766 field = FindFieldFromCode(field_idx, referrer, self, true, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800767 if (LIKELY(field != NULL)) {
768 return field->GetObj(NULL);
769 }
770 return NULL; // Will throw exception by checking with Thread::Current
771}
772
773extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
774 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800775 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800776 if (LIKELY(field != NULL && obj != NULL)) {
777 return field->Get32(obj);
778 }
779 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800780 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800781 if (LIKELY(field != NULL)) {
782 if (UNLIKELY(obj == NULL)) {
783 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700784 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800785 return field->Get32(obj);
786 }
787 }
788 return 0; // Will throw exception by checking with Thread::Current
789}
790
791extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
792 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800793 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800794 if (LIKELY(field != NULL && obj != NULL)) {
795 return field->Get64(obj);
796 }
797 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800798 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800799 if (LIKELY(field != NULL)) {
800 if (UNLIKELY(obj == NULL)) {
801 ThrowNullPointerExceptionForFieldAccess(self, field, true);
802 } else {
803 return field->Get64(obj);
804 }
805 }
806 return 0; // Will throw exception by checking with Thread::Current
807}
808
809extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
810 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800811 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800812 if (LIKELY(field != NULL && obj != NULL)) {
813 return field->GetObj(obj);
814 }
815 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800816 field = FindFieldFromCode(field_idx, referrer, self, false, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800817 if (LIKELY(field != NULL)) {
818 if (UNLIKELY(obj == NULL)) {
819 ThrowNullPointerExceptionForFieldAccess(self, field, true);
820 } else {
821 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700822 }
823 }
824 return NULL; // Will throw exception by checking with Thread::Current
825}
826
Ian Rogers1bddec32012-02-04 12:27:34 -0800827extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
828 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800829 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700830 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800831 field->Set32(NULL, new_value);
832 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700833 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700834 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800835 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800836 if (LIKELY(field != NULL)) {
837 field->Set32(NULL, new_value);
838 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700839 }
840 return -1; // failure
841}
842
843extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
844 uint64_t new_value, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800845 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700846 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800847 field->Set64(NULL, new_value);
848 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700849 }
850 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800851 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700852 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800853 field->Set64(NULL, new_value);
854 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700855 }
856 return -1; // failure
857}
858
Ian Rogers1bddec32012-02-04 12:27:34 -0800859extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
860 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800861 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700862 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800863 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700864 field->SetObj(NULL, new_value);
865 return 0; // success
866 }
867 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700868 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800869 field = FindFieldFromCode(field_idx, referrer, self, true, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800870 if (LIKELY(field != NULL)) {
871 field->SetObj(NULL, new_value);
872 return 0; // success
873 }
874 return -1; // failure
875}
876
877extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
878 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800879 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800880 if (LIKELY(field != NULL && obj != NULL)) {
881 field->Set32(obj, new_value);
882 return 0; // success
883 }
884 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800885 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800886 if (LIKELY(field != NULL)) {
887 if (UNLIKELY(obj == NULL)) {
888 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700889 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800890 field->Set32(obj, new_value);
891 return 0; // success
892 }
893 }
894 return -1; // failure
895}
896
897extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
898 Thread* self, Method** sp) {
899 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
900 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
jeffhao8cd6dda2012-02-22 10:15:34 -0800901 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800902 if (LIKELY(field != NULL && obj != NULL)) {
903 field->Set64(obj, new_value);
904 return 0; // success
905 }
906 *sp = callee_save;
907 self->SetTopOfStack(sp, 0);
jeffhao8cd6dda2012-02-22 10:15:34 -0800908 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800909 if (LIKELY(field != NULL)) {
910 if (UNLIKELY(obj == NULL)) {
911 ThrowNullPointerExceptionForFieldAccess(self, field, false);
912 } else {
913 field->Set64(obj, new_value);
914 return 0; // success
915 }
916 }
917 return -1; // failure
918}
919
920extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
921 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800922 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800923 if (LIKELY(field != NULL && obj != NULL)) {
924 field->SetObj(obj, new_value);
925 return 0; // success
926 }
927 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800928 field = FindFieldFromCode(field_idx, referrer, self, false, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800929 if (LIKELY(field != NULL)) {
930 if (UNLIKELY(obj == NULL)) {
931 ThrowNullPointerExceptionForFieldAccess(self, field, false);
932 } else {
933 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700934 return 0; // success
935 }
936 }
937 return -1; // failure
938}
939
Shih-wei Liao2d831012011-09-28 22:06:53 -0700940// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
941// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800942// When verification/compiler hasn't been able to verify access, optionally perform an access
943// check.
944static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
945 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700946 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700947 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700948 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700949 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700950 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700951 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700952 return NULL; // Failure
953 }
954 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800955 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800956 if (UNLIKELY(!klass->IsInstantiable())) {
957 self->ThrowNewException("Ljava/lang/InstantiationError;",
958 PrettyDescriptor(klass).c_str());
959 return NULL; // Failure
960 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800961 Class* referrer = method->GetDeclaringClass();
962 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800963 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800964 return NULL; // Failure
965 }
966 }
buzbee33a129c2011-10-06 16:53:20 -0700967 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
968 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700969 return NULL; // Failure
970 }
971 return klass->AllocObject();
972}
973
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800974extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
975 Thread* self, Method** sp) {
976 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
977 return AllocObjectFromCode(type_idx, method, self, false);
978}
979
Ian Rogers28ad40d2011-10-27 15:19:26 -0700980extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
981 Thread* self, Method** sp) {
982 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800983 return AllocObjectFromCode(type_idx, method, self, true);
984}
985
986// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
987// it cannot be resolved, throw an error. If it can, use it to create an array.
988// When verification/compiler hasn't been able to verify access, optionally perform an access
989// check.
990static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
991 Thread* self, bool access_check) {
992 if (UNLIKELY(component_count < 0)) {
993 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
994 component_count);
995 return NULL; // Failure
996 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700997 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800998 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
999 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
1000 if (klass == NULL) { // Error
1001 DCHECK(Thread::Current()->IsExceptionPending());
1002 return NULL; // Failure
1003 }
1004 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
1005 }
1006 if (access_check) {
1007 Class* referrer = method->GetDeclaringClass();
1008 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001009 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001010 return NULL; // Failure
1011 }
1012 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001013 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -07001014}
1015
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001016extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
1017 Thread* self, Method** sp) {
1018 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1019 return AllocArrayFromCode(type_idx, method, component_count, self, false);
1020}
1021
1022extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
1023 int32_t component_count,
1024 Thread* self, Method** sp) {
1025 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1026 return AllocArrayFromCode(type_idx, method, component_count, self, true);
1027}
1028
1029// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -07001030Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001031 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001032 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -07001033 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001034 return NULL; // Failure
1035 }
1036 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001037 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -07001038 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
1039 if (klass == NULL) { // Error
1040 DCHECK(Thread::Current()->IsExceptionPending());
1041 return NULL; // Failure
1042 }
1043 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001044 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001045 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001046 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -07001047 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001048 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001049 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001050 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -07001051 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001052 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001053 }
1054 return NULL; // Failure
1055 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001056 if (access_check) {
1057 Class* referrer = method->GetDeclaringClass();
1058 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001059 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001060 return NULL; // Failure
1061 }
1062 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001063 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001064 return Array::Alloc(klass, component_count);
1065 }
1066}
1067
Ian Rogersce9eca62011-10-07 17:11:03 -07001068extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
1069 int32_t component_count, Thread* self, Method** sp) {
1070 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001071 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -07001072}
1073
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001074extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
1075 int32_t component_count,
1076 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001077 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -08001078 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001079}
1080
Ian Rogerscaab8c42011-10-12 12:11:18 -07001081// Assignable test for code, won't throw. Null and equality tests already performed
1082uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
1083 DCHECK(klass != NULL);
1084 DCHECK(ref_class != NULL);
1085 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1086}
1087
Shih-wei Liao2d831012011-09-28 22:06:53 -07001088// 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 -07001089extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001090 DCHECK(a->IsClass()) << PrettyClass(a);
1091 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001092 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001093 return 0; // Success
1094 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001095 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001096 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -07001097 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001098 PrettyDescriptor(a).c_str(),
1099 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001100 return -1; // Failure
1101 }
1102}
1103
1104// Tests whether 'element' can be assigned into an array of type 'array_class'.
1105// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001106extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
1107 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001108 DCHECK(array_class != NULL);
1109 // element can't be NULL as we catch this is screened in runtime_support
1110 Class* element_class = element->GetClass();
1111 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -07001112 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -07001113 return 0; // Success
1114 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001115 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001116 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -08001117 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001118 PrettyDescriptor(element_class).c_str(),
1119 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -07001120 return -1; // Failure
1121 }
1122}
1123
Elliott Hughesf3778f62012-01-26 14:14:35 -08001124Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
1125 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001126 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1127 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001128 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001129 CHECK(self->IsExceptionPending());
1130 return NULL; // Failure - Indicate to caller to deliver exception
1131 }
Elliott Hughesf3778f62012-01-26 14:14:35 -08001132 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001133 Class* referring_class = referrer->GetDeclaringClass();
1134 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
1135 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001136 return NULL; // Failure - Indicate to caller to deliver exception
1137 }
1138 // If we're just implementing const-class, we shouldn't call <clinit>.
1139 if (!can_run_clinit) {
1140 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -07001141 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001142 // If we are the <clinit> of this class, just return our storage.
1143 //
1144 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
1145 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001146 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001147 return klass;
1148 }
1149 if (!class_linker->EnsureInitialized(klass, true)) {
1150 CHECK(self->IsExceptionPending());
1151 return NULL; // Failure - Indicate to caller to deliver exception
1152 }
1153 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
1154 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001155}
1156
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001157extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
1158 Thread* self, Method** sp) {
1159 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001160 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001161}
1162
Ian Rogers28ad40d2011-10-27 15:19:26 -07001163extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
1164 Method** sp) {
1165 // Called when method->dex_cache_resolved_types_[] misses
1166 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001167 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001168}
1169
Ian Rogersb093c6b2011-10-31 16:19:55 -07001170extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
1171 const Method* referrer, Thread* self,
1172 Method** sp) {
1173 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
1174 // unpopulated
1175 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001176 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -07001177}
1178
Brian Carlstromaded5f72011-10-07 17:15:04 -07001179String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1180 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1181 return class_linker->ResolveString(string_idx, referrer);
1182}
1183
1184extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1185 Thread* self, Method** sp) {
1186 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1187 return ResolveStringFromCode(referrer, string_idx);
1188}
1189
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001190extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1191 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001192 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001193 // MonitorExit may throw exception
1194 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1195}
1196
1197extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1198 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1199 DCHECK(obj != NULL); // Assumed to have been checked before entry
1200 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001201 DCHECK(thread->HoldsLock(obj));
1202 // Only possible exception is NPE and is handled before entry
1203 DCHECK(!thread->IsExceptionPending());
1204}
1205
Ian Rogers4a510d82011-10-09 14:30:24 -07001206void CheckSuspendFromCode(Thread* thread) {
1207 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001208 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1209}
1210
Ian Rogers4a510d82011-10-09 14:30:24 -07001211extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1212 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001213 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001214 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1215}
1216
1217/*
1218 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1219 * not of sufficient length.
1220 *
1221 * NOTE: When dealing with a raw dex file, the data to be copied uses
1222 * little-endian ordering. Require that oat2dex do any required swapping
1223 * so this routine can get by with a memcpy().
1224 *
1225 * Format of the data:
1226 * ushort ident = 0x0300 magic value
1227 * ushort width width of each element in the table
1228 * uint size number of elements in the table
1229 * ubyte data[size*width] table of data values (may contain a single-byte
1230 * padding at the end)
1231 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001232extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1233 Thread* self, Method** sp) {
1234 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001235 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001236 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001237 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1238 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001239 return -1; // Error
1240 }
1241 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1242 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001243 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001244 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1245 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001246 return -1; // Error
1247 }
1248 uint16_t width = table[1];
1249 uint32_t size_in_bytes = size * width;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001250 memcpy((char*)array + Array::DataOffset(width).Int32Value(), (char*)&table[4], size_in_bytes);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001251 return 0; // Success
1252}
1253
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001254// Fast path method resolution that can't throw exceptions
1255static Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001256 bool access_check, InvokeType type) {
1257 bool is_direct = type == kStatic || type == kDirect;
1258 if (UNLIKELY(this_object == NULL && !is_direct)) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001259 return NULL;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001260 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001261 Method* resolved_method =
1262 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
1263 if (UNLIKELY(resolved_method == NULL)) {
1264 return NULL;
1265 }
1266 if (access_check) {
1267 Class* methods_class = resolved_method->GetDeclaringClass();
1268 Class* referring_class = referrer->GetDeclaringClass();
1269 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1270 !referring_class->CanAccessMember(methods_class,
1271 resolved_method->GetAccessFlags()))) {
1272 // potential illegal access
1273 return NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -07001274 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001275 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001276 if (type == kInterface) { // Most common form of slow path dispatch.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001277 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001278 } else if (is_direct) {
1279 return resolved_method;
1280 } else if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001281 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1282 } else {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001283 DCHECK(type == kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001284 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1285 }
1286}
1287
1288// Slow path method resolution
1289static Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001290 Thread* self, bool access_check, InvokeType type) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001291 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001292 bool is_direct = type == kStatic || type == kDirect;
1293 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
1294 if (UNLIKELY(resolved_method == NULL)) {
1295 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
1296 return NULL; // failure
1297 } else {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001298 if (!access_check) {
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 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
1317 } else {
1318 vtable = this_object->GetClass()->GetVTable();
1319 }
1320 // TODO: eliminate bounds check?
1321 return vtable->Get(vtable_index);
1322 }
1323 } else {
1324 Class* methods_class = resolved_method->GetDeclaringClass();
1325 Class* referring_class = referrer->GetDeclaringClass();
1326 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1327 !referring_class->CanAccessMember(methods_class,
1328 resolved_method->GetAccessFlags()))) {
1329 // The referring class can't access the resolved method, this may occur as a result of a
1330 // protected method being made public by implementing an interface that re-declares the
1331 // method public. Resort to the dex file to determine the correct class for the access check
1332 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
1333 methods_class = class_linker->ResolveType(dex_file,
1334 dex_file.GetMethodId(method_idx).class_idx_,
1335 referring_class);
1336 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
1337 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001338 referrer, resolved_method, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001339 return NULL; // failure
1340 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
1341 resolved_method->GetAccessFlags()))) {
1342 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
1343 return NULL; // failure
1344 }
1345 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001346 if (is_direct) {
1347 return resolved_method;
1348 } else if (type == kInterface) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001349 Method* interface_method =
1350 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1351 if (UNLIKELY(interface_method == NULL)) {
1352 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1353 resolved_method,
1354 this_object);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001355 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001356 } else {
1357 return interface_method;
1358 }
1359 } else {
1360 ObjectArray<Method>* vtable;
1361 uint16_t vtable_index = resolved_method->GetMethodIndex();
Ian Rogersc8b306f2012-02-17 21:34:44 -08001362 if (type == kSuper) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001363 Class* super_class = referring_class->GetSuperClass();
1364 if (LIKELY(super_class != NULL)) {
1365 vtable = referring_class->GetSuperClass()->GetVTable();
1366 } else {
1367 vtable = NULL;
1368 }
1369 } else {
1370 vtable = this_object->GetClass()->GetVTable();
1371 }
1372 if (LIKELY(vtable != NULL &&
1373 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
1374 return vtable->GetWithoutChecks(vtable_index);
1375 } else {
1376 // Behavior to agree with that of the verifier
1377 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
1378 "attempt to invoke %s method '%s' from '%s'"
1379 " using incorrect form of method dispatch",
Ian Rogersc8b306f2012-02-17 21:34:44 -08001380 (type == kSuper ? "super class" : "virtual"),
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001381 PrettyMethod(resolved_method).c_str(),
1382 PrettyMethod(referrer).c_str());
Ian Rogersc8b306f2012-02-17 21:34:44 -08001383 return NULL; // failure
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001384 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001385 }
1386 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001387 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001388}
1389
1390static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001391 Thread* self, Method** sp, bool access_check, InvokeType type){
1392 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001393 if (UNLIKELY(method == NULL)) {
1394 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001395 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
1396 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001397 return 0; // failure
1398 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001399 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001400 if (UNLIKELY(method == NULL)) {
1401 CHECK(self->IsExceptionPending());
1402 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001403 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001404 }
Ian Rogers19846512012-02-24 11:42:47 -08001405 DCHECK(!self->IsExceptionPending());
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001406 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001407
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001408 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001409 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1410 uint64_t result = ((code_uint << 32) | method_uint);
1411 return result;
1412}
1413
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001414// See comments in runtime_support_asm.S
1415extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1416 Method* caller_method, Thread* self,
1417 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001418 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001419}
1420
1421extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1422 Object* this_object,
1423 Method* caller_method, Thread* self,
1424 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001425 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1426}
1427
1428
1429extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1430 Object* this_object,
1431 Method* caller_method, Thread* self,
1432 Method** sp) {
1433 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1434}
1435
1436extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1437 Object* this_object,
1438 Method* caller_method, Thread* self,
1439 Method** sp) {
1440 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001441}
1442
1443extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1444 Object* this_object,
1445 Method* caller_method, Thread* self,
1446 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001447 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001448}
1449
1450extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1451 Object* this_object,
1452 Method* caller_method, Thread* self,
1453 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001454 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001455}
1456
Ian Rogers466bb252011-10-14 03:29:56 -07001457static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1458 ScopedLocalRef<jclass> jlr_UTE_class(env,
1459 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1460 if (jlr_UTE_class.get() == NULL) {
1461 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1462 } else {
1463 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1464 "(Ljava/lang/Throwable;)V");
1465 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1466 ScopedLocalRef<jthrowable> jlr_UTE(env,
1467 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1468 jexception)));
1469 int rc = env->Throw(jlr_UTE.get());
1470 if (rc != JNI_OK) {
1471 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1472 }
1473 }
1474 CHECK(self->IsExceptionPending());
1475}
1476
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001477// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1478// which is responsible for recording callee save registers. We explicitly handlerize incoming
1479// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1480// the invocation handler which is a field within the proxy object receiver.
1481extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001482 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001483 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001484 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1485 DCHECK_EQ(*proxy_sp, proxy_method);
1486 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001487 // TODO: ARM specific
1488 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1489 // Start new JNI local reference state
1490 JNIEnvExt* env = self->GetJniEnv();
1491 ScopedJniEnvLocalRefState env_state(env);
1492 // Create local ref. copies of proxy method and the receiver
1493 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1494 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1495
Ian Rogers14b1b242011-10-11 18:54:34 -07001496 // Placing into local references incoming arguments from the caller's register arguments,
1497 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001498 MethodHelper proxy_mh(proxy_method);
1499 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001500 size_t args_in_regs = 0;
1501 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001502 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001503 if (args_in_regs > 2) {
1504 args_in_regs = 2;
1505 break;
1506 }
1507 }
1508 size_t cur_arg = 0; // current stack location to read
1509 size_t param_index = 1; // skip receiver
1510 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001511 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001512 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001513 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001514 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001515 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001516 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001517 param_index++;
1518 }
1519 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001520 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001521 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001522 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001523 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1524 jobject jobj = AddLocalReference<jobject>(env, obj);
1525 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001526 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001527 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001528 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001529 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001530 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1531 jvalue args_jobj[3];
1532 args_jobj[0].l = rcvr_jobj;
1533 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001534 // Args array, if no arguments then NULL (don't include receiver in argument count)
1535 args_jobj[2].l = NULL;
1536 ObjectArray<Object>* args = NULL;
1537 if ((num_params - 1) > 0) {
1538 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001539 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001540 CHECK(self->IsExceptionPending());
1541 return;
1542 }
1543 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1544 }
1545 // Convert proxy method into expected interface method
1546 Method* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers19846512012-02-24 11:42:47 -08001547 DCHECK(interface_method != NULL);
1548 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001549 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001550 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001551 cur_arg = 0; // reset stack location to read to start
1552 // reset index, will index into param type array which doesn't include the receiver
1553 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001554 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers19846512012-02-24 11:42:47 -08001555 DCHECK(param_types != NULL);
Ian Rogers14b1b242011-10-11 18:54:34 -07001556 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
Ian Rogers19846512012-02-24 11:42:47 -08001557 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001558 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1559 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001560 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001561 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001562 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001563 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001564 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1565 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1566 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001567 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001568 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001569 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001570 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001571 if (self->IsExceptionPending()) {
1572 return;
1573 }
1574 obj = val.l;
1575 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001576 args->Set(param_index, obj);
1577 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1578 param_index++;
1579 }
1580 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001581 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1582 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001583 Class* param_type = param_types->Get(param_index);
1584 Object* obj;
1585 if (!param_type->IsPrimitive()) {
1586 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1587 } else {
1588 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001589 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001590 if (self->IsExceptionPending()) {
1591 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001592 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001593 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001594 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001595 args->Set(param_index, obj);
1596 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1597 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001598 }
1599 // Get the InvocationHandler method and the field that holds it within the Proxy object
1600 static jmethodID inv_hand_invoke_mid = NULL;
1601 static jfieldID proxy_inv_hand_fid = NULL;
1602 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001603 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001604 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001605 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001606 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1607 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1608 }
Ian Rogers466bb252011-10-14 03:29:56 -07001609 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001610 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1611 // Call InvocationHandler.invoke
1612 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1613 // Place result in stack args
1614 if (!self->IsExceptionPending()) {
1615 Object* result_ref = self->DecodeJObject(result);
1616 if (result_ref != NULL) {
1617 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001618 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001619 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1620 } else {
1621 *reinterpret_cast<jobject*>(stack_args) = NULL;
1622 }
Ian Rogers466bb252011-10-14 03:29:56 -07001623 } else {
1624 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1625 // a UndeclaredThrowableException.
1626 Throwable* exception = self->GetException();
1627 self->ClearException();
1628 if (!exception->IsCheckedException()) {
1629 self->SetException(exception);
1630 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001631 SynthesizedProxyClass* proxy_class =
1632 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1633 int throws_index = -1;
1634 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1635 for (size_t i = 0; i < num_virt_methods; i++) {
1636 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1637 throws_index = i;
1638 break;
1639 }
1640 }
1641 CHECK_NE(throws_index, -1);
1642 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001643 Class* exception_class = exception->GetClass();
1644 bool declares_exception = false;
1645 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1646 Class* declared_exception = declared_exceptions->Get(i);
1647 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1648 }
1649 if (declares_exception) {
1650 self->SetException(exception);
1651 } else {
1652 ThrowNewUndeclaredThrowableException(self, env, exception);
1653 }
1654 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001655 }
1656}
1657
jeffhaoe343b762011-12-05 16:36:44 -08001658extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001659 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001660 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1661 self->PushTraceStackFrame(trace_frame);
1662
jeffhao2692b572011-12-16 15:42:28 -08001663 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001664
jeffhao2692b572011-12-16 15:42:28 -08001665 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001666}
1667
1668extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001669 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001670 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1671 Method* method = trace_frame.method_;
1672 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001673
jeffhao2692b572011-12-16 15:42:28 -08001674 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001675
1676 return lr;
1677}
1678
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001679uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001680 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001681 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1682 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001683 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001684
jeffhao2692b572011-12-16 15:42:28 -08001685 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001686
1687 return lr;
1688}
1689
Shih-wei Liao2d831012011-09-28 22:06:53 -07001690/*
1691 * Float/double conversion requires clamping to min and max of integer form. If
1692 * target doesn't support this normally, use these.
1693 */
1694int64_t D2L(double d) {
1695 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1696 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1697 if (d >= kMaxLong)
1698 return (int64_t)0x7fffffffffffffffULL;
1699 else if (d <= kMinLong)
1700 return (int64_t)0x8000000000000000ULL;
1701 else if (d != d) // NaN case
1702 return 0;
1703 else
1704 return (int64_t)d;
1705}
1706
1707int64_t F2L(float f) {
1708 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1709 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1710 if (f >= kMaxLong)
1711 return (int64_t)0x7fffffffffffffffULL;
1712 else if (f <= kMinLong)
1713 return (int64_t)0x8000000000000000ULL;
1714 else if (f != f) // NaN case
1715 return 0;
1716 else
1717 return (int64_t)f;
1718}
1719
1720} // namespace art