blob: 1ac92fd3430e825f251edc909d823b1adf4caaad [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
Ian Rogerscaab8c42011-10-12 12:11:18 -070019#include "dex_cache.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070020#include "dex_verifier.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070021#include "macros.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object.h"
23#include "object_utils.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070024#include "reflection.h"
jeffhaoe343b762011-12-05 16:36:44 -080025#include "trace.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070026#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070027
Shih-wei Liao2d831012011-09-28 22:06:53 -070028namespace art {
29
Ian Rogers4f0d07c2011-10-06 23:38:47 -070030// Place a special frame at the TOS that will save the callee saves for the given type
31static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070032 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070033 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
34 self->SetTopOfStack(sp, 0);
35}
36
Ian Rogersa32a6fd2012-02-06 20:18:44 -080037static void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed) {
38 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
39 "illegal class access: '%s' -> '%s'",
40 PrettyDescriptor(referrer).c_str(),
41 PrettyDescriptor(accessed).c_str());
42}
43
44static void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer,
45 Class* accessed, const Method* caller,
46 const Method* called,
47 bool is_interface, bool is_super) {
48 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
49 "illegal class access ('%s' -> '%s')"
50 "in attempt to invoke %s method '%s' from '%s'",
51 PrettyDescriptor(referrer).c_str(),
52 PrettyDescriptor(accessed).c_str(),
53 (is_interface ? "interface" : (is_super ? "super class" : "virtual")),
54 PrettyMethod(called).c_str(),
55 PrettyMethod(caller).c_str());
56}
57
58static void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
59 const Method* referrer,
60 const Method* interface_method,
61 Object* this_object) {
62 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
63 "class '%s' does not implement interface '%s' in call to '%s' from '%s'",
64 PrettyDescriptor(this_object->GetClass()).c_str(),
65 PrettyDescriptor(interface_method->GetDeclaringClass()).c_str(),
66 PrettyMethod(interface_method).c_str(), PrettyMethod(referrer).c_str());
67}
68
69static void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed) {
70 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
71 "Field '%s' is inaccessible to class '%s'",
72 PrettyField(accessed, false).c_str(),
73 PrettyDescriptor(referrer).c_str());
74}
75
76static void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed) {
77 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
78 "Method '%s' is inaccessible to class '%s'",
79 PrettyMethod(accessed).c_str(),
80 PrettyDescriptor(referrer).c_str());
81}
82
83static void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read) {
84 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
85 "Attempt to %s field '%s' on a null object reference",
86 is_read ? "read from" : "write to",
87 PrettyField(field, true).c_str());
88}
89
90static void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller,
91 uint32_t method_idx, bool is_interface,
92 bool is_super) {
93 const DexFile& dex_file =
94 Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
95 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
96 "Attempt to invoke %s method '%s' from '%s' on a null object reference",
97 (is_interface ? "interface" : (is_super ? "super class" : "virtual")),
98 PrettyMethod(method_idx, dex_file, true).c_str(),
99 PrettyMethod(caller).c_str());
100}
101
buzbee44b412b2012-02-04 08:50:53 -0800102/*
103 * Report location to debugger. Note: dalvikPC is the current offset within
104 * the method. However, because the offset alone cannot distinguish between
105 * method entry and offset 0 within the method, we'll use an offset of -1
106 * to denote method entry.
107 */
108extern "C" void artUpdateDebuggerFromCode(int32_t dalvikPC, Thread* self, Method** sp) {
109 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
110 // TODO: fill this out similar to old "updateDebugger"
111}
112
Shih-wei Liao2d831012011-09-28 22:06:53 -0700113// Temporary debugging hook for compiler.
114extern void DebugMe(Method* method, uint32_t info) {
115 LOG(INFO) << "DebugMe";
116 if (method != NULL) {
117 LOG(INFO) << PrettyMethod(method);
118 }
119 LOG(INFO) << "Info: " << info;
120}
121
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -0700122extern "C" uint32_t artObjectInitFromCode(Object* o, Thread* self, Method** sp) {
123 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700124 Class* c = o->GetClass();
125 if (UNLIKELY(c->IsFinalizable())) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700126 Heap::AddFinalizerReference(self, o);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700127 }
128 /*
129 * NOTE: once debugger/profiler support is added, we'll need to check
130 * here and branch to actual compiled object.<init> to handle any
Ian Rogers0571d352011-11-03 19:51:38 -0700131 * breakpoint/logging activities if either is active.
Ian Rogerscaab8c42011-10-12 12:11:18 -0700132 */
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -0700133 return self->IsExceptionPending() ? -1 : 0;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700134}
135
Shih-wei Liao2d831012011-09-28 22:06:53 -0700136// Return value helper for jobject return types
137extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -0700138 if (thread->IsExceptionPending()) {
139 return NULL;
140 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700141 return thread->DecodeJObject(obj);
142}
143
144extern void* FindNativeMethod(Thread* thread) {
145 DCHECK(Thread::Current() == thread);
146
147 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
148 DCHECK(method != NULL);
149
150 // Lookup symbol address for method, on failure we'll return NULL with an
151 // exception set, otherwise we return the address of the method we found.
152 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
153 if (native_code == NULL) {
154 DCHECK(thread->IsExceptionPending());
155 return NULL;
156 } else {
157 // Register so that future calls don't come here
158 method->RegisterNative(native_code);
159 return native_code;
160 }
161}
162
163// Called by generated call to throw an exception
164extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
165 /*
166 * exception may be NULL, in which case this routine should
167 * throw NPE. NOTE: this is a convenience for generated code,
168 * which previously did the null check inline and constructed
169 * and threw a NPE if NULL. This routine responsible for setting
170 * exception_ in thread and delivering the exception.
171 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700172 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700173 if (exception == NULL) {
174 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
175 } else {
176 thread->SetException(exception);
177 }
178 thread->DeliverException();
179}
180
181// Deliver an exception that's pending on thread helping set up a callee save frame on the way
182extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700183 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700184 thread->DeliverException();
185}
186
187// Called by generated call to throw a NPE exception
188extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700189 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700190 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700191 thread->DeliverException();
192}
193
194// Called by generated call to throw an arithmetic divide by zero exception
195extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700196 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700197 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
198 thread->DeliverException();
199}
200
201// Called by generated call to throw an arithmetic divide by zero exception
202extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700203 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
204 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
205 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700206 thread->DeliverException();
207}
208
209// Called by the AbstractMethodError stub (not runtime support)
210extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700211 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
212 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
213 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700214 thread->DeliverException();
215}
216
217extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700218 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800219 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800220 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800221 artTraceMethodUnwindFromCode(thread);
222 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700223 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700224 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
225 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700226 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700227 thread->ResetDefaultStackEnd(); // Return to default stack size
228 thread->DeliverException();
229}
230
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700231static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700232 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700233 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
234 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
235
236 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700237 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700238 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
239 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700240 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700241 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
242 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700243 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700244 type_idx = ref;
245 } else {
246 CHECK(false) << static_cast<int>(ref_type);
247 }
248
Ian Rogers0571d352011-11-03 19:51:38 -0700249 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700250 if (!access) {
251 return class_name;
252 }
253
254 std::string result;
255 result += "tried to access class ";
256 result += class_name;
257 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700259 return result;
260}
261
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700262static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700263 verifier::VerifyErrorRefType ref_type, bool access) {
264 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700265
266 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
267 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
268
269 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700270 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700271 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700272 if (!access) {
273 return class_name + "." + field_name;
274 }
275
276 std::string result;
277 result += "tried to access field ";
278 result += class_name + "." + field_name;
279 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700281 return result;
282}
283
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700284static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700285 verifier::VerifyErrorRefType ref_type, bool access) {
286 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700287
288 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
289 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
290
291 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700292 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700293 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700294 if (!access) {
295 return class_name + "." + method_name;
296 }
297
298 std::string result;
299 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700300 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700301 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700302 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700304 return result;
305}
306
307extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700308 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
309 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700310 frame.Next();
311 Method* method = frame.GetMethod();
312
Ian Rogersd81871c2011-10-03 13:57:23 -0700313 verifier::VerifyErrorRefType ref_type =
314 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700315
316 const char* exception_class = "Ljava/lang/VerifyError;";
317 std::string msg;
318
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
320 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700321 exception_class = "Ljava/lang/NoClassDefFoundError;";
322 msg = ClassNameFromIndex(method, ref, ref_type, false);
323 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700324 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700325 exception_class = "Ljava/lang/NoSuchFieldError;";
326 msg = FieldNameFromIndex(method, ref, ref_type, false);
327 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700328 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700329 exception_class = "Ljava/lang/NoSuchMethodError;";
330 msg = MethodNameFromIndex(method, ref, ref_type, false);
331 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700332 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700333 exception_class = "Ljava/lang/IllegalAccessError;";
334 msg = ClassNameFromIndex(method, ref, ref_type, true);
335 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700336 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700337 exception_class = "Ljava/lang/IllegalAccessError;";
338 msg = FieldNameFromIndex(method, ref, ref_type, true);
339 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700340 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700341 exception_class = "Ljava/lang/IllegalAccessError;";
342 msg = MethodNameFromIndex(method, ref, ref_type, true);
343 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700344 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700345 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
346 msg = ClassNameFromIndex(method, ref, ref_type, false);
347 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700348 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700349 exception_class = "Ljava/lang/InstantiationError;";
350 msg = ClassNameFromIndex(method, ref, ref_type, false);
351 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700352 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700353 // Generic VerifyError; use default exception, no message.
354 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700355 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700356 CHECK(false);
357 break;
358 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700359 self->ThrowNewException(exception_class, msg.c_str());
360 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700361}
362
363extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700364 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700365 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700366 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700367 thread->DeliverException();
368}
369
370extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700371 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700372 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700373 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700374 thread->DeliverException();
375}
376
Elliott Hughese1410a22011-10-04 12:10:24 -0700377extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700378 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
379 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700380 frame.Next();
381 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700382 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700383 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700384 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700385}
386
387extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700388 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700389 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700390 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700391 thread->DeliverException();
392}
393
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700394void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700395 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700396 // TODO: this code is specific to ARM
397 // On entry the stack pointed by sp is:
398 // | argN | |
399 // | ... | |
400 // | arg4 | |
401 // | arg3 spill | | Caller's frame
402 // | arg2 spill | |
403 // | arg1 spill | |
404 // | Method* | ---
405 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700406 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700407 // | R3 | arg3
408 // | R2 | arg2
409 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700410 // | R0 |
411 // | Method* | <- sp
412 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
413 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
414 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
415 uintptr_t caller_pc = regs[10];
416 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700417 // Start new JNI local reference state
418 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700419 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700420 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700421 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700422 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
423 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700424 size_t args_in_regs = 0;
425 for (size_t i = 1; i < shorty_len; i++) {
426 char c = shorty[i];
427 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
428 if (args_in_regs > 3) {
429 args_in_regs = 3;
430 break;
431 }
432 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700433 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700434 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700435 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700436 // less two as return address may span into next dex instruction
437 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800438 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700439 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
440 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700441 Instruction::Code instr_code = instr->Opcode();
442 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
443 (instr_code == Instruction::INVOKE_STATIC_RANGE);
444 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
445 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700446 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700447 is_static = type == Runtime::kStaticMethod;
448 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700449 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700450 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700451 if (!is_static) {
452 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
453 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700454 if (args_in_regs < 3) {
455 // If we thought we had fewer than 3 arguments in registers, account for the receiver
456 args_in_regs++;
457 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700458 AddLocalReference<jobject>(env, obj);
459 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700460 size_t shorty_index = 1; // skip return value
461 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
462 // R0)
463 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
464 char c = shorty[shorty_index];
465 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700466 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700467 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
468 AddLocalReference<jobject>(env, obj);
469 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700470 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
471 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700472 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700473 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700474 while (shorty_index < shorty_len) {
475 char c = shorty[shorty_index];
476 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700477 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700478 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700479 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700480 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700481 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700482 }
483 // Resolve method filling in dex cache
484 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700485 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800486 if (LIKELY(called->IsDirect())) {
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800487 // Ensure that the called method's class is initialized
488 Class* called_class = called->GetDeclaringClass();
489 linker->EnsureInitialized(called_class, true);
490 if (LIKELY(called_class->IsInitialized())) {
491 // Update CodeAndDirectMethod table and avoid the trampoline when we know the called class
492 // is initialized (see test 084-class-init SlowInit)
493 Method* caller = *caller_sp;
494 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
495 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
496 // We got this far, ensure that the declaring class is initialized
497 linker->EnsureInitialized(called->GetDeclaringClass(), true);
498 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800499 } else {
500 // Direct method has been made virtual
501 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
502 "Expected direct method but found virtual: %s",
503 PrettyMethod(called, true).c_str());
504 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700505 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700506 void* code;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700507 if (UNLIKELY(thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700508 // Something went wrong in ResolveMethod or EnsureInitialized,
509 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700510 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700511 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700512 thread->ClearException();
513 } else {
514 // Expect class to at least be initializing
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800515 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogersad25ac52011-10-04 19:13:33 -0700516 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700517 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700518 code = const_cast<void*>(called->GetCode());
519 }
520 return code;
521}
522
Ian Rogerscaab8c42011-10-12 12:11:18 -0700523// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800524static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
525 size_t expected_size) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800526 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700527 if (UNLIKELY(resolved_field == NULL)) {
528 return NULL;
529 }
530 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800531 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700532 if (UNLIKELY(!fields_class->IsInitializing())) {
533 return NULL;
534 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800535 Class* referring_class = referrer->GetDeclaringClass();
536 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
537 !referring_class->CanAccessMember(fields_class,
538 resolved_field->GetAccessFlags()))) {
539 // illegal access
540 return NULL;
541 }
542 FieldHelper fh(resolved_field);
543 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
544 fh.FieldSize() != expected_size)) {
545 return NULL;
546 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700547 return resolved_field;
548}
549
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800550
Ian Rogerscaab8c42011-10-12 12:11:18 -0700551// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800552Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
553 bool is_static, bool is_primitive, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700554 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700555 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
556 if (LIKELY(resolved_field != NULL)) {
557 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800558 Class* referring_class = referrer->GetDeclaringClass();
559 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800560 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
Ian Rogers1bddec32012-02-04 12:27:34 -0800561 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
562 resolved_field->GetAccessFlags()))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800563 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
Ian Rogers1bddec32012-02-04 12:27:34 -0800564 return NULL; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -0700565 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800566 FieldHelper fh(resolved_field);
567 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
568 fh.FieldSize() != expected_size)) {
569 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughes1e409252012-02-06 11:21:27 -0800570 "Attempted read of %zd-bit %s on field '%s'",
Ian Rogers1bddec32012-02-04 12:27:34 -0800571 expected_size * (32 / sizeof(int32_t)),
572 is_primitive ? "primitive" : "non-primitive",
573 PrettyField(resolved_field, true).c_str());
574 }
575 if (!is_static) {
576 // instance fields must be being accessed on an initialized class
Ian Rogerscaab8c42011-10-12 12:11:18 -0700577 return resolved_field;
Ian Rogers1bddec32012-02-04 12:27:34 -0800578 } else {
579 // If the class is already initializing, we must be inside <clinit>, or
580 // we'd still be waiting for the lock.
581 if (fields_class->IsInitializing()) {
582 return resolved_field;
583 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
584 return resolved_field;
585 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700586 }
587 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800588 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
Ian Rogersce9eca62011-10-07 17:11:03 -0700589 return NULL;
590}
591
Ian Rogersce9eca62011-10-07 17:11:03 -0700592extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
593 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800594 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700595 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800596 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700597 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700598 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800599 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
600 if (LIKELY(field != NULL)) {
601 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700602 }
603 return 0; // Will throw exception by checking with Thread::Current
604}
605
606extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
607 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800608 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700609 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800610 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700611 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700612 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800613 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
614 if (LIKELY(field != NULL)) {
615 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700616 }
617 return 0; // Will throw exception by checking with Thread::Current
618}
619
620extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
621 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800622 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700623 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800624 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700625 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700626 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800627 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
628 if (LIKELY(field != NULL)) {
629 return field->GetObj(NULL);
630 }
631 return NULL; // Will throw exception by checking with Thread::Current
632}
633
634extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
635 const Method* referrer, Thread* self, Method** sp) {
636 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
637 if (LIKELY(field != NULL && obj != NULL)) {
638 return field->Get32(obj);
639 }
640 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
641 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
642 if (LIKELY(field != NULL)) {
643 if (UNLIKELY(obj == NULL)) {
644 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700645 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800646 return field->Get32(obj);
647 }
648 }
649 return 0; // Will throw exception by checking with Thread::Current
650}
651
652extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
653 const Method* referrer, Thread* self, Method** sp) {
654 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
655 if (LIKELY(field != NULL && obj != NULL)) {
656 return field->Get64(obj);
657 }
658 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
659 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
660 if (LIKELY(field != NULL)) {
661 if (UNLIKELY(obj == NULL)) {
662 ThrowNullPointerExceptionForFieldAccess(self, field, true);
663 } else {
664 return field->Get64(obj);
665 }
666 }
667 return 0; // Will throw exception by checking with Thread::Current
668}
669
670extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
671 const Method* referrer, Thread* self, Method** sp) {
672 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
673 if (LIKELY(field != NULL && obj != NULL)) {
674 return field->GetObj(obj);
675 }
676 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
677 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
678 if (LIKELY(field != NULL)) {
679 if (UNLIKELY(obj == NULL)) {
680 ThrowNullPointerExceptionForFieldAccess(self, field, true);
681 } else {
682 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700683 }
684 }
685 return NULL; // Will throw exception by checking with Thread::Current
686}
687
Ian Rogers1bddec32012-02-04 12:27:34 -0800688extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
689 const Method* referrer, Thread* self, Method** sp) {
690 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700691 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800692 field->Set32(NULL, new_value);
693 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700694 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700695 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800696 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
697 if (LIKELY(field != NULL)) {
698 field->Set32(NULL, new_value);
699 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700700 }
701 return -1; // failure
702}
703
704extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
705 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800706 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700707 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800708 field->Set64(NULL, new_value);
709 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700710 }
711 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800712 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700713 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800714 field->Set64(NULL, new_value);
715 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700716 }
717 return -1; // failure
718}
719
Ian Rogers1bddec32012-02-04 12:27:34 -0800720extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
721 const Method* referrer, Thread* self, Method** sp) {
722 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700723 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800724 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700725 field->SetObj(NULL, new_value);
726 return 0; // success
727 }
728 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700729 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800730 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
731 if (LIKELY(field != NULL)) {
732 field->SetObj(NULL, new_value);
733 return 0; // success
734 }
735 return -1; // failure
736}
737
738extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
739 const Method* referrer, Thread* self, Method** sp) {
740 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
741 if (LIKELY(field != NULL && obj != NULL)) {
742 field->Set32(obj, new_value);
743 return 0; // success
744 }
745 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
746 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
747 if (LIKELY(field != NULL)) {
748 if (UNLIKELY(obj == NULL)) {
749 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700750 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800751 field->Set32(obj, new_value);
752 return 0; // success
753 }
754 }
755 return -1; // failure
756}
757
758extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
759 Thread* self, Method** sp) {
760 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
761 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
762 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
763 if (LIKELY(field != NULL && obj != NULL)) {
764 field->Set64(obj, new_value);
765 return 0; // success
766 }
767 *sp = callee_save;
768 self->SetTopOfStack(sp, 0);
769 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
770 if (LIKELY(field != NULL)) {
771 if (UNLIKELY(obj == NULL)) {
772 ThrowNullPointerExceptionForFieldAccess(self, field, false);
773 } else {
774 field->Set64(obj, new_value);
775 return 0; // success
776 }
777 }
778 return -1; // failure
779}
780
781extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
782 const Method* referrer, Thread* self, Method** sp) {
783 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
784 if (LIKELY(field != NULL && obj != NULL)) {
785 field->SetObj(obj, new_value);
786 return 0; // success
787 }
788 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
789 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
790 if (LIKELY(field != NULL)) {
791 if (UNLIKELY(obj == NULL)) {
792 ThrowNullPointerExceptionForFieldAccess(self, field, false);
793 } else {
794 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700795 return 0; // success
796 }
797 }
798 return -1; // failure
799}
800
Shih-wei Liao2d831012011-09-28 22:06:53 -0700801// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
802// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800803// When verification/compiler hasn't been able to verify access, optionally perform an access
804// check.
805static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
806 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700807 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700808 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700809 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700810 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700811 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700812 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700813 return NULL; // Failure
814 }
815 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800816 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800817 if (UNLIKELY(!klass->IsInstantiable())) {
818 self->ThrowNewException("Ljava/lang/InstantiationError;",
819 PrettyDescriptor(klass).c_str());
820 return NULL; // Failure
821 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800822 Class* referrer = method->GetDeclaringClass();
823 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800824 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800825 return NULL; // Failure
826 }
827 }
buzbee33a129c2011-10-06 16:53:20 -0700828 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
829 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700830 return NULL; // Failure
831 }
832 return klass->AllocObject();
833}
834
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800835extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
836 Thread* self, Method** sp) {
837 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
838 return AllocObjectFromCode(type_idx, method, self, false);
839}
840
Ian Rogers28ad40d2011-10-27 15:19:26 -0700841extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
842 Thread* self, Method** sp) {
843 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800844 return AllocObjectFromCode(type_idx, method, self, true);
845}
846
847// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
848// it cannot be resolved, throw an error. If it can, use it to create an array.
849// When verification/compiler hasn't been able to verify access, optionally perform an access
850// check.
851static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
852 Thread* self, bool access_check) {
853 if (UNLIKELY(component_count < 0)) {
854 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
855 component_count);
856 return NULL; // Failure
857 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700858 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800859 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
860 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
861 if (klass == NULL) { // Error
862 DCHECK(Thread::Current()->IsExceptionPending());
863 return NULL; // Failure
864 }
865 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
866 }
867 if (access_check) {
868 Class* referrer = method->GetDeclaringClass();
869 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800870 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700871 return NULL; // Failure
872 }
873 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800874 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700875}
876
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800877extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
878 Thread* self, Method** sp) {
879 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
880 return AllocArrayFromCode(type_idx, method, component_count, self, false);
881}
882
883extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
884 int32_t component_count,
885 Thread* self, Method** sp) {
886 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
887 return AllocArrayFromCode(type_idx, method, component_count, self, true);
888}
889
890// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700891Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800892 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700893 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700894 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700895 return NULL; // Failure
896 }
897 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700898 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700899 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
900 if (klass == NULL) { // Error
901 DCHECK(Thread::Current()->IsExceptionPending());
902 return NULL; // Failure
903 }
904 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700905 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700906 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700907 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700908 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700910 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700911 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700912 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800913 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700914 }
915 return NULL; // Failure
916 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800917 if (access_check) {
918 Class* referrer = method->GetDeclaringClass();
919 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800920 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800921 return NULL; // Failure
922 }
923 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700924 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700925 return Array::Alloc(klass, component_count);
926 }
927}
928
Ian Rogersce9eca62011-10-07 17:11:03 -0700929extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
930 int32_t component_count, Thread* self, Method** sp) {
931 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800932 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700933}
934
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800935extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
936 int32_t component_count,
937 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700938 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800939 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700940}
941
Ian Rogerscaab8c42011-10-12 12:11:18 -0700942// Assignable test for code, won't throw. Null and equality tests already performed
943uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
944 DCHECK(klass != NULL);
945 DCHECK(ref_class != NULL);
946 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
947}
948
Shih-wei Liao2d831012011-09-28 22:06:53 -0700949// 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 -0700950extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700951 DCHECK(a->IsClass()) << PrettyClass(a);
952 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700953 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700954 return 0; // Success
955 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700956 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700957 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700958 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800959 PrettyDescriptor(a).c_str(),
960 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700961 return -1; // Failure
962 }
963}
964
965// Tests whether 'element' can be assigned into an array of type 'array_class'.
966// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700967extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
968 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700969 DCHECK(array_class != NULL);
970 // element can't be NULL as we catch this is screened in runtime_support
971 Class* element_class = element->GetClass();
972 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700973 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700974 return 0; // Success
975 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700976 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700977 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800978 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800979 PrettyDescriptor(element_class).c_str(),
980 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700981 return -1; // Failure
982 }
983}
984
Elliott Hughesf3778f62012-01-26 14:14:35 -0800985Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
986 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700987 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
988 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700989 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700990 CHECK(self->IsExceptionPending());
991 return NULL; // Failure - Indicate to caller to deliver exception
992 }
Elliott Hughesf3778f62012-01-26 14:14:35 -0800993 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800994 Class* referring_class = referrer->GetDeclaringClass();
995 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
996 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800997 return NULL; // Failure - Indicate to caller to deliver exception
998 }
999 // If we're just implementing const-class, we shouldn't call <clinit>.
1000 if (!can_run_clinit) {
1001 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -07001002 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001003 // If we are the <clinit> of this class, just return our storage.
1004 //
1005 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
1006 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001007 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001008 return klass;
1009 }
1010 if (!class_linker->EnsureInitialized(klass, true)) {
1011 CHECK(self->IsExceptionPending());
1012 return NULL; // Failure - Indicate to caller to deliver exception
1013 }
1014 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
1015 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001016}
1017
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001018extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
1019 Thread* self, Method** sp) {
1020 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001021 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001022}
1023
Ian Rogers28ad40d2011-10-27 15:19:26 -07001024extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
1025 Method** sp) {
1026 // Called when method->dex_cache_resolved_types_[] misses
1027 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001028 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001029}
1030
Ian Rogersb093c6b2011-10-31 16:19:55 -07001031extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
1032 const Method* referrer, Thread* self,
1033 Method** sp) {
1034 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
1035 // unpopulated
1036 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001037 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -07001038}
1039
buzbee48d72222012-01-11 15:19:51 -08001040// Helper function to resolve virtual method
1041extern "C" Method* artResolveMethodFromCode(Method* referrer,
1042 uint32_t method_idx,
1043 bool is_direct,
1044 Thread* self,
1045 Method** sp) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001046 /*
1047 * Slow-path handler on invoke virtual method path in which
buzbee48d72222012-01-11 15:19:51 -08001048 * base method is unresolved at compile-time. Caller will
1049 * unwind if can't resolve.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001050 */
buzbee48d72222012-01-11 15:19:51 -08001051 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1052 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1053 Method* method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
1054 referrer->GetDexCacheResolvedMethods()->Set(method_idx, method);
1055 return method;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001056}
1057
Brian Carlstromaded5f72011-10-07 17:15:04 -07001058String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1059 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1060 return class_linker->ResolveString(string_idx, referrer);
1061}
1062
1063extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1064 Thread* self, Method** sp) {
1065 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1066 return ResolveStringFromCode(referrer, string_idx);
1067}
1068
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001069extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1070 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001071 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001072 // MonitorExit may throw exception
1073 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1074}
1075
1076extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1077 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1078 DCHECK(obj != NULL); // Assumed to have been checked before entry
1079 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001080 DCHECK(thread->HoldsLock(obj));
1081 // Only possible exception is NPE and is handled before entry
1082 DCHECK(!thread->IsExceptionPending());
1083}
1084
Ian Rogers4a510d82011-10-09 14:30:24 -07001085void CheckSuspendFromCode(Thread* thread) {
1086 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001087 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1088}
1089
Ian Rogers4a510d82011-10-09 14:30:24 -07001090extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1091 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001092 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001093 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1094}
1095
1096/*
1097 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1098 * not of sufficient length.
1099 *
1100 * NOTE: When dealing with a raw dex file, the data to be copied uses
1101 * little-endian ordering. Require that oat2dex do any required swapping
1102 * so this routine can get by with a memcpy().
1103 *
1104 * Format of the data:
1105 * ushort ident = 0x0300 magic value
1106 * ushort width width of each element in the table
1107 * uint size number of elements in the table
1108 * ubyte data[size*width] table of data values (may contain a single-byte
1109 * padding at the end)
1110 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001111extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1112 Thread* self, Method** sp) {
1113 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001114 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001115 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001116 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1117 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001118 return -1; // Error
1119 }
1120 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1121 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001122 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001123 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1124 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001125 return -1; // Error
1126 }
1127 uint16_t width = table[1];
1128 uint32_t size_in_bytes = size * width;
1129 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
1130 return 0; // Success
1131}
1132
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001133// Fast path method resolution that can't throw exceptions
1134static Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
1135 bool access_check, bool is_interface, bool is_super) {
1136 if (UNLIKELY(this_object == NULL)) {
1137 return NULL;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001138 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001139 Method* resolved_method =
1140 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
1141 if (UNLIKELY(resolved_method == NULL)) {
1142 return NULL;
1143 }
1144 if (access_check) {
1145 Class* methods_class = resolved_method->GetDeclaringClass();
1146 Class* referring_class = referrer->GetDeclaringClass();
1147 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1148 !referring_class->CanAccessMember(methods_class,
1149 resolved_method->GetAccessFlags()))) {
1150 // potential illegal access
1151 return NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -07001152 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001153 }
1154 if (is_interface) {
1155 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1156 } else if (is_super) {
1157 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1158 } else {
1159 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1160 }
1161}
1162
1163// Slow path method resolution
1164static Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
1165 Thread* self, bool access_check, bool is_interface,
1166 bool is_super) {
1167 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1168 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, false);
1169 if (LIKELY(resolved_method != NULL)) {
1170 if (!access_check) {
1171 if (is_interface) {
1172 Method* interface_method =
1173 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1174 if (UNLIKELY(interface_method == NULL)) {
1175 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1176 resolved_method,
1177 this_object);
1178 return NULL;
1179 } else {
1180 return interface_method;
1181 }
1182 } else {
1183 ObjectArray<Method>* vtable;
1184 uint16_t vtable_index = resolved_method->GetMethodIndex();
1185 if (is_super) {
1186 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
1187 } else {
1188 vtable = this_object->GetClass()->GetVTable();
1189 }
1190 // TODO: eliminate bounds check?
1191 return vtable->Get(vtable_index);
1192 }
1193 } else {
1194 Class* methods_class = resolved_method->GetDeclaringClass();
1195 Class* referring_class = referrer->GetDeclaringClass();
1196 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1197 !referring_class->CanAccessMember(methods_class,
1198 resolved_method->GetAccessFlags()))) {
1199 // The referring class can't access the resolved method, this may occur as a result of a
1200 // protected method being made public by implementing an interface that re-declares the
1201 // method public. Resort to the dex file to determine the correct class for the access check
1202 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
1203 methods_class = class_linker->ResolveType(dex_file,
1204 dex_file.GetMethodId(method_idx).class_idx_,
1205 referring_class);
1206 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
1207 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
1208 referrer, resolved_method, is_interface,
1209 is_super);
1210 return NULL; // failure
1211 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
1212 resolved_method->GetAccessFlags()))) {
1213 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
1214 return NULL; // failure
1215 }
1216 }
1217 if (is_interface) {
1218 Method* interface_method =
1219 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1220 if (UNLIKELY(interface_method == NULL)) {
1221 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1222 resolved_method,
1223 this_object);
1224 return NULL;
1225 } else {
1226 return interface_method;
1227 }
1228 } else {
1229 ObjectArray<Method>* vtable;
1230 uint16_t vtable_index = resolved_method->GetMethodIndex();
1231 if (is_super) {
1232 Class* super_class = referring_class->GetSuperClass();
1233 if (LIKELY(super_class != NULL)) {
1234 vtable = referring_class->GetSuperClass()->GetVTable();
1235 } else {
1236 vtable = NULL;
1237 }
1238 } else {
1239 vtable = this_object->GetClass()->GetVTable();
1240 }
1241 if (LIKELY(vtable != NULL &&
1242 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
1243 return vtable->GetWithoutChecks(vtable_index);
1244 } else {
1245 // Behavior to agree with that of the verifier
1246 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
1247 "attempt to invoke %s method '%s' from '%s'"
1248 " using incorrect form of method dispatch",
1249 (is_super ? "super class" : "virtual"),
1250 PrettyMethod(resolved_method).c_str(),
1251 PrettyMethod(referrer).c_str());
1252 return NULL;
1253 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001254 }
1255 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001256 }
1257 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
1258 return NULL;
1259}
1260
1261static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
1262 Thread* self, Method** sp, bool access_check, bool is_interface,
1263 bool is_super){
1264 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1265 is_interface, is_super);
1266 if (UNLIKELY(method == NULL)) {
1267 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1268 if (UNLIKELY(this_object == NULL)) {
1269 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, is_interface,
1270 is_super);
1271 return 0; // failure
1272 }
1273 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check,
1274 is_interface, is_super);
1275 if (UNLIKELY(method == NULL)) {
1276 CHECK(self->IsExceptionPending());
1277 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001278 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001279 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001280 // TODO: DCHECK
1281 CHECK(!self->IsExceptionPending());
1282 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001283
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001284 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001285 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1286 uint64_t result = ((code_uint << 32) | method_uint);
1287 return result;
1288}
1289
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001290// See comments in runtime_support_asm.S
1291extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1292 Method* caller_method, Thread* self,
1293 Method** sp) {
1294 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, true, false);
1295}
1296
1297extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1298 Object* this_object,
1299 Method* caller_method, Thread* self,
1300 Method** sp) {
1301 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, true, false);
1302}
1303
1304extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1305 Object* this_object,
1306 Method* caller_method, Thread* self,
1307 Method** sp) {
1308 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, false, true);
1309}
1310
1311extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1312 Object* this_object,
1313 Method* caller_method, Thread* self,
1314 Method** sp) {
1315 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, false, false);
1316}
1317
Ian Rogers466bb252011-10-14 03:29:56 -07001318static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1319 ScopedLocalRef<jclass> jlr_UTE_class(env,
1320 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1321 if (jlr_UTE_class.get() == NULL) {
1322 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1323 } else {
1324 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1325 "(Ljava/lang/Throwable;)V");
1326 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1327 ScopedLocalRef<jthrowable> jlr_UTE(env,
1328 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1329 jexception)));
1330 int rc = env->Throw(jlr_UTE.get());
1331 if (rc != JNI_OK) {
1332 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1333 }
1334 }
1335 CHECK(self->IsExceptionPending());
1336}
1337
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001338// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1339// which is responsible for recording callee save registers. We explicitly handlerize incoming
1340// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1341// the invocation handler which is a field within the proxy object receiver.
1342extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001343 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001344 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001345 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1346 DCHECK_EQ(*proxy_sp, proxy_method);
1347 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001348 // TODO: ARM specific
1349 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1350 // Start new JNI local reference state
1351 JNIEnvExt* env = self->GetJniEnv();
1352 ScopedJniEnvLocalRefState env_state(env);
1353 // Create local ref. copies of proxy method and the receiver
1354 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1355 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1356
Ian Rogers14b1b242011-10-11 18:54:34 -07001357 // Placing into local references incoming arguments from the caller's register arguments,
1358 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001359 MethodHelper proxy_mh(proxy_method);
1360 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001361 size_t args_in_regs = 0;
1362 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001363 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001364 if (args_in_regs > 2) {
1365 args_in_regs = 2;
1366 break;
1367 }
1368 }
1369 size_t cur_arg = 0; // current stack location to read
1370 size_t param_index = 1; // skip receiver
1371 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001372 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001373 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001374 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001375 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001376 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001377 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001378 param_index++;
1379 }
1380 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001381 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001382 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001383 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001384 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1385 jobject jobj = AddLocalReference<jobject>(env, obj);
1386 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001387 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001388 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001389 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001390 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001391 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1392 jvalue args_jobj[3];
1393 args_jobj[0].l = rcvr_jobj;
1394 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001395 // Args array, if no arguments then NULL (don't include receiver in argument count)
1396 args_jobj[2].l = NULL;
1397 ObjectArray<Object>* args = NULL;
1398 if ((num_params - 1) > 0) {
1399 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001400 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001401 CHECK(self->IsExceptionPending());
1402 return;
1403 }
1404 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1405 }
1406 // Convert proxy method into expected interface method
1407 Method* interface_method = proxy_method->FindOverriddenMethod();
1408 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001409 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001410 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001411 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001412 cur_arg = 0; // reset stack location to read to start
1413 // reset index, will index into param type array which doesn't include the receiver
1414 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001415 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001416 CHECK(param_types != NULL);
1417 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1418 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1419 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1420 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001421 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001422 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001423 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001424 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001425 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1426 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1427 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001428 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001429 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001430 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001431 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001432 if (self->IsExceptionPending()) {
1433 return;
1434 }
1435 obj = val.l;
1436 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001437 args->Set(param_index, obj);
1438 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1439 param_index++;
1440 }
1441 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001442 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1443 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001444 Class* param_type = param_types->Get(param_index);
1445 Object* obj;
1446 if (!param_type->IsPrimitive()) {
1447 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1448 } else {
1449 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001450 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001451 if (self->IsExceptionPending()) {
1452 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001453 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001454 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001455 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001456 args->Set(param_index, obj);
1457 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1458 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001459 }
1460 // Get the InvocationHandler method and the field that holds it within the Proxy object
1461 static jmethodID inv_hand_invoke_mid = NULL;
1462 static jfieldID proxy_inv_hand_fid = NULL;
1463 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001464 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001465 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001466 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001467 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1468 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1469 }
Ian Rogers466bb252011-10-14 03:29:56 -07001470 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001471 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1472 // Call InvocationHandler.invoke
1473 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1474 // Place result in stack args
1475 if (!self->IsExceptionPending()) {
1476 Object* result_ref = self->DecodeJObject(result);
1477 if (result_ref != NULL) {
1478 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001479 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001480 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1481 } else {
1482 *reinterpret_cast<jobject*>(stack_args) = NULL;
1483 }
Ian Rogers466bb252011-10-14 03:29:56 -07001484 } else {
1485 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1486 // a UndeclaredThrowableException.
1487 Throwable* exception = self->GetException();
1488 self->ClearException();
1489 if (!exception->IsCheckedException()) {
1490 self->SetException(exception);
1491 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001492 SynthesizedProxyClass* proxy_class =
1493 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1494 int throws_index = -1;
1495 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1496 for (size_t i = 0; i < num_virt_methods; i++) {
1497 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1498 throws_index = i;
1499 break;
1500 }
1501 }
1502 CHECK_NE(throws_index, -1);
1503 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001504 Class* exception_class = exception->GetClass();
1505 bool declares_exception = false;
1506 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1507 Class* declared_exception = declared_exceptions->Get(i);
1508 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1509 }
1510 if (declares_exception) {
1511 self->SetException(exception);
1512 } else {
1513 ThrowNewUndeclaredThrowableException(self, env, exception);
1514 }
1515 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001516 }
1517}
1518
jeffhaoe343b762011-12-05 16:36:44 -08001519extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001520 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001521 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1522 self->PushTraceStackFrame(trace_frame);
1523
jeffhao2692b572011-12-16 15:42:28 -08001524 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001525
jeffhao2692b572011-12-16 15:42:28 -08001526 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001527}
1528
1529extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001530 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001531 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1532 Method* method = trace_frame.method_;
1533 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001534
jeffhao2692b572011-12-16 15:42:28 -08001535 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001536
1537 return lr;
1538}
1539
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001540uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001541 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001542 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1543 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001544 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001545
jeffhao2692b572011-12-16 15:42:28 -08001546 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001547
1548 return lr;
1549}
1550
Shih-wei Liao2d831012011-09-28 22:06:53 -07001551/*
1552 * Float/double conversion requires clamping to min and max of integer form. If
1553 * target doesn't support this normally, use these.
1554 */
1555int64_t D2L(double d) {
1556 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1557 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1558 if (d >= kMaxLong)
1559 return (int64_t)0x7fffffffffffffffULL;
1560 else if (d <= kMinLong)
1561 return (int64_t)0x8000000000000000ULL;
1562 else if (d != d) // NaN case
1563 return 0;
1564 else
1565 return (int64_t)d;
1566}
1567
1568int64_t F2L(float f) {
1569 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1570 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1571 if (f >= kMaxLong)
1572 return (int64_t)0x7fffffffffffffffULL;
1573 else if (f <= kMinLong)
1574 return (int64_t)0x8000000000000000ULL;
1575 else if (f != f) // NaN case
1576 return 0;
1577 else
1578 return (int64_t)f;
1579}
1580
1581} // namespace art