blob: 670049a3c47f063a96d5ea147957aaba38ef1b2a [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
2 * Copyright 2011 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "runtime_support.h"
18
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080019#include "debugger.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070020#include "dex_cache.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070021#include "dex_verifier.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070022#include "macros.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080023#include "object.h"
24#include "object_utils.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070025#include "reflection.h"
jeffhaoe343b762011-12-05 16:36:44 -080026#include "trace.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070027#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070028
Shih-wei Liao2d831012011-09-28 22:06:53 -070029namespace art {
30
Ian Rogers4f0d07c2011-10-06 23:38:47 -070031// Place a special frame at the TOS that will save the callee saves for the given type
32static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070033 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070034 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
35 self->SetTopOfStack(sp, 0);
36}
37
Ian Rogersa32a6fd2012-02-06 20:18:44 -080038static void ThrowNewIllegalAccessErrorClass(Thread* self, Class* referrer, Class* accessed) {
39 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
40 "illegal class access: '%s' -> '%s'",
41 PrettyDescriptor(referrer).c_str(),
42 PrettyDescriptor(accessed).c_str());
43}
44
45static void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, Class* referrer,
46 Class* accessed, const Method* caller,
47 const Method* called,
48 bool is_interface, bool is_super) {
49 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
50 "illegal class access ('%s' -> '%s')"
51 "in attempt to invoke %s method '%s' from '%s'",
52 PrettyDescriptor(referrer).c_str(),
53 PrettyDescriptor(accessed).c_str(),
54 (is_interface ? "interface" : (is_super ? "super class" : "virtual")),
55 PrettyMethod(called).c_str(),
56 PrettyMethod(caller).c_str());
57}
58
59static void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self,
60 const Method* referrer,
61 const Method* interface_method,
62 Object* this_object) {
63 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
64 "class '%s' does not implement interface '%s' in call to '%s' from '%s'",
65 PrettyDescriptor(this_object->GetClass()).c_str(),
66 PrettyDescriptor(interface_method->GetDeclaringClass()).c_str(),
67 PrettyMethod(interface_method).c_str(), PrettyMethod(referrer).c_str());
68}
69
70static void ThrowNewIllegalAccessErrorField(Thread* self, Class* referrer, Field* accessed) {
71 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
72 "Field '%s' is inaccessible to class '%s'",
73 PrettyField(accessed, false).c_str(),
74 PrettyDescriptor(referrer).c_str());
75}
76
77static void ThrowNewIllegalAccessErrorMethod(Thread* self, Class* referrer, Method* accessed) {
78 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
79 "Method '%s' is inaccessible to class '%s'",
80 PrettyMethod(accessed).c_str(),
81 PrettyDescriptor(referrer).c_str());
82}
83
84static void ThrowNullPointerExceptionForFieldAccess(Thread* self, Field* field, bool is_read) {
85 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
86 "Attempt to %s field '%s' on a null object reference",
87 is_read ? "read from" : "write to",
88 PrettyField(field, true).c_str());
89}
90
91static void ThrowNullPointerExceptionForMethodAccess(Thread* self, Method* caller,
92 uint32_t method_idx, bool is_interface,
93 bool is_super) {
94 const DexFile& dex_file =
95 Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
96 self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
97 "Attempt to invoke %s method '%s' from '%s' on a null object reference",
98 (is_interface ? "interface" : (is_super ? "super class" : "virtual")),
99 PrettyMethod(method_idx, dex_file, true).c_str(),
100 PrettyMethod(caller).c_str());
101}
102
buzbee44b412b2012-02-04 08:50:53 -0800103/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800104 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -0800105 * the method. However, because the offset alone cannot distinguish between
106 * method entry and offset 0 within the method, we'll use an offset of -1
107 * to denote method entry.
108 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800109extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -0800110 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800111 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -0800112}
113
Shih-wei Liao2d831012011-09-28 22:06:53 -0700114// Temporary debugging hook for compiler.
115extern void DebugMe(Method* method, uint32_t info) {
116 LOG(INFO) << "DebugMe";
117 if (method != NULL) {
118 LOG(INFO) << PrettyMethod(method);
119 }
120 LOG(INFO) << "Info: " << info;
121}
122
123// Return value helper for jobject return types
124extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -0700125 if (thread->IsExceptionPending()) {
126 return NULL;
127 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700128 return thread->DecodeJObject(obj);
129}
130
131extern void* FindNativeMethod(Thread* thread) {
132 DCHECK(Thread::Current() == thread);
133
134 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
135 DCHECK(method != NULL);
136
137 // Lookup symbol address for method, on failure we'll return NULL with an
138 // exception set, otherwise we return the address of the method we found.
139 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
140 if (native_code == NULL) {
141 DCHECK(thread->IsExceptionPending());
142 return NULL;
143 } else {
144 // Register so that future calls don't come here
145 method->RegisterNative(native_code);
146 return native_code;
147 }
148}
149
150// Called by generated call to throw an exception
151extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
152 /*
153 * exception may be NULL, in which case this routine should
154 * throw NPE. NOTE: this is a convenience for generated code,
155 * which previously did the null check inline and constructed
156 * and threw a NPE if NULL. This routine responsible for setting
157 * exception_ in thread and delivering the exception.
158 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700159 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700160 if (exception == NULL) {
161 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
162 } else {
163 thread->SetException(exception);
164 }
165 thread->DeliverException();
166}
167
168// Deliver an exception that's pending on thread helping set up a callee save frame on the way
169extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700170 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700171 thread->DeliverException();
172}
173
174// Called by generated call to throw a NPE exception
175extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700176 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700177 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700178 thread->DeliverException();
179}
180
181// Called by generated call to throw an arithmetic divide by zero exception
182extern "C" void artThrowDivZeroFromCode(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->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
185 thread->DeliverException();
186}
187
188// Called by generated call to throw an arithmetic divide by zero exception
189extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700190 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
191 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
192 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700193 thread->DeliverException();
194}
195
196// Called by the AbstractMethodError stub (not runtime support)
197extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700198 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
199 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
200 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700201 thread->DeliverException();
202}
203
204extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700205 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800206 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800207 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800208 artTraceMethodUnwindFromCode(thread);
209 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700210 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700211 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
212 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700213 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700214 thread->ResetDefaultStackEnd(); // Return to default stack size
215 thread->DeliverException();
216}
217
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700218static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700219 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700220 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
221 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
222
223 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700224 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700225 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
226 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700227 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700228 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
229 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700230 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700231 type_idx = ref;
232 } else {
233 CHECK(false) << static_cast<int>(ref_type);
234 }
235
Ian Rogers0571d352011-11-03 19:51:38 -0700236 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700237 if (!access) {
238 return class_name;
239 }
240
241 std::string result;
242 result += "tried to access class ";
243 result += class_name;
244 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800245 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700246 return result;
247}
248
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700249static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700250 verifier::VerifyErrorRefType ref_type, bool access) {
251 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700252
253 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
254 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
255
256 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700257 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700258 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700259 if (!access) {
260 return class_name + "." + field_name;
261 }
262
263 std::string result;
264 result += "tried to access field ";
265 result += class_name + "." + field_name;
266 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700268 return result;
269}
270
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700271static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700272 verifier::VerifyErrorRefType ref_type, bool access) {
273 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700274
275 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
276 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
277
278 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700279 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700280 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700281 if (!access) {
282 return class_name + "." + method_name;
283 }
284
285 std::string result;
286 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700287 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700288 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700289 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800290 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700291 return result;
292}
293
294extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700295 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
296 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700297 frame.Next();
298 Method* method = frame.GetMethod();
299
Ian Rogersd81871c2011-10-03 13:57:23 -0700300 verifier::VerifyErrorRefType ref_type =
301 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700302
303 const char* exception_class = "Ljava/lang/VerifyError;";
304 std::string msg;
305
Ian Rogersd81871c2011-10-03 13:57:23 -0700306 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
307 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700308 exception_class = "Ljava/lang/NoClassDefFoundError;";
309 msg = ClassNameFromIndex(method, ref, ref_type, false);
310 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700311 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700312 exception_class = "Ljava/lang/NoSuchFieldError;";
313 msg = FieldNameFromIndex(method, ref, ref_type, false);
314 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700315 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700316 exception_class = "Ljava/lang/NoSuchMethodError;";
317 msg = MethodNameFromIndex(method, ref, ref_type, false);
318 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700320 exception_class = "Ljava/lang/IllegalAccessError;";
321 msg = ClassNameFromIndex(method, ref, ref_type, true);
322 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700323 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700324 exception_class = "Ljava/lang/IllegalAccessError;";
325 msg = FieldNameFromIndex(method, ref, ref_type, true);
326 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700328 exception_class = "Ljava/lang/IllegalAccessError;";
329 msg = MethodNameFromIndex(method, ref, ref_type, true);
330 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700331 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700332 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
333 msg = ClassNameFromIndex(method, ref, ref_type, false);
334 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700335 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700336 exception_class = "Ljava/lang/InstantiationError;";
337 msg = ClassNameFromIndex(method, ref, ref_type, false);
338 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700339 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700340 // Generic VerifyError; use default exception, no message.
341 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700342 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700343 CHECK(false);
344 break;
345 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700346 self->ThrowNewException(exception_class, msg.c_str());
347 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700348}
349
350extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700351 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700352 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700353 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700354 thread->DeliverException();
355}
356
357extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700358 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700359 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700360 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700361 thread->DeliverException();
362}
363
Elliott Hughese1410a22011-10-04 12:10:24 -0700364extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700365 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
366 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700367 frame.Next();
368 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700369 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700370 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700371 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700372}
373
374extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700375 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700376 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700377 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700378 thread->DeliverException();
379}
380
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700381void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700382 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700383 // TODO: this code is specific to ARM
384 // On entry the stack pointed by sp is:
385 // | argN | |
386 // | ... | |
387 // | arg4 | |
388 // | arg3 spill | | Caller's frame
389 // | arg2 spill | |
390 // | arg1 spill | |
391 // | Method* | ---
392 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700393 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700394 // | R3 | arg3
395 // | R2 | arg2
396 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700397 // | R0 |
398 // | Method* | <- sp
399 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
400 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
401 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
402 uintptr_t caller_pc = regs[10];
403 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700404 // Start new JNI local reference state
405 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700406 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700407 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700408 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700409 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
410 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700411 size_t args_in_regs = 0;
412 for (size_t i = 1; i < shorty_len; i++) {
413 char c = shorty[i];
414 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
415 if (args_in_regs > 3) {
416 args_in_regs = 3;
417 break;
418 }
419 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700420 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700421 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700422 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700423 // less two as return address may span into next dex instruction
424 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800425 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700426 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
427 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700428 Instruction::Code instr_code = instr->Opcode();
429 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
430 (instr_code == Instruction::INVOKE_STATIC_RANGE);
431 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
432 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700433 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700434 is_static = type == Runtime::kStaticMethod;
435 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700436 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700437 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700438 if (!is_static) {
439 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
440 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700441 if (args_in_regs < 3) {
442 // If we thought we had fewer than 3 arguments in registers, account for the receiver
443 args_in_regs++;
444 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700445 AddLocalReference<jobject>(env, obj);
446 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700447 size_t shorty_index = 1; // skip return value
448 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
449 // R0)
450 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
451 char c = shorty[shorty_index];
452 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700453 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700454 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
455 AddLocalReference<jobject>(env, obj);
456 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700457 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
458 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700459 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700460 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700461 while (shorty_index < shorty_len) {
462 char c = shorty[shorty_index];
463 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700464 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700465 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700466 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700467 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700468 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700469 }
470 // Resolve method filling in dex cache
471 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700472 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800473 if (LIKELY(called->IsDirect())) {
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800474 // Ensure that the called method's class is initialized
475 Class* called_class = called->GetDeclaringClass();
476 linker->EnsureInitialized(called_class, true);
477 if (LIKELY(called_class->IsInitialized())) {
478 // Update CodeAndDirectMethod table and avoid the trampoline when we know the called class
479 // is initialized (see test 084-class-init SlowInit)
480 Method* caller = *caller_sp;
481 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
482 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
483 // We got this far, ensure that the declaring class is initialized
484 linker->EnsureInitialized(called->GetDeclaringClass(), true);
485 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800486 } else {
487 // Direct method has been made virtual
488 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
489 "Expected direct method but found virtual: %s",
490 PrettyMethod(called, true).c_str());
491 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700492 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700493 void* code;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700494 if (UNLIKELY(thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700495 // Something went wrong in ResolveMethod or EnsureInitialized,
496 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700497 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700498 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700499 thread->ClearException();
500 } else {
501 // Expect class to at least be initializing
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800502 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogersad25ac52011-10-04 19:13:33 -0700503 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700504 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700505 code = const_cast<void*>(called->GetCode());
506 }
507 return code;
508}
509
Ian Rogerscaab8c42011-10-12 12:11:18 -0700510// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800511static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
512 size_t expected_size) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800513 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700514 if (UNLIKELY(resolved_field == NULL)) {
515 return NULL;
516 }
517 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800518 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700519 if (UNLIKELY(!fields_class->IsInitializing())) {
520 return NULL;
521 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800522 Class* referring_class = referrer->GetDeclaringClass();
523 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
524 !referring_class->CanAccessMember(fields_class,
525 resolved_field->GetAccessFlags()))) {
526 // illegal access
527 return NULL;
528 }
529 FieldHelper fh(resolved_field);
530 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
531 fh.FieldSize() != expected_size)) {
532 return NULL;
533 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700534 return resolved_field;
535}
536
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800537
Ian Rogerscaab8c42011-10-12 12:11:18 -0700538// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800539Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
540 bool is_static, bool is_primitive, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700541 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700542 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
543 if (LIKELY(resolved_field != NULL)) {
544 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800545 Class* referring_class = referrer->GetDeclaringClass();
546 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800547 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
Ian Rogers1bddec32012-02-04 12:27:34 -0800548 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
549 resolved_field->GetAccessFlags()))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800550 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
Ian Rogers1bddec32012-02-04 12:27:34 -0800551 return NULL; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -0700552 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800553 FieldHelper fh(resolved_field);
554 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
555 fh.FieldSize() != expected_size)) {
556 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughes1e409252012-02-06 11:21:27 -0800557 "Attempted read of %zd-bit %s on field '%s'",
Ian Rogers1bddec32012-02-04 12:27:34 -0800558 expected_size * (32 / sizeof(int32_t)),
559 is_primitive ? "primitive" : "non-primitive",
560 PrettyField(resolved_field, true).c_str());
561 }
562 if (!is_static) {
563 // instance fields must be being accessed on an initialized class
Ian Rogerscaab8c42011-10-12 12:11:18 -0700564 return resolved_field;
Ian Rogers1bddec32012-02-04 12:27:34 -0800565 } else {
566 // If the class is already initializing, we must be inside <clinit>, or
567 // we'd still be waiting for the lock.
568 if (fields_class->IsInitializing()) {
569 return resolved_field;
570 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
571 return resolved_field;
572 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700573 }
574 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800575 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
Ian Rogersce9eca62011-10-07 17:11:03 -0700576 return NULL;
577}
578
Ian Rogersce9eca62011-10-07 17:11:03 -0700579extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
580 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800581 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700582 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800583 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700584 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700585 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800586 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
587 if (LIKELY(field != NULL)) {
588 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700589 }
590 return 0; // Will throw exception by checking with Thread::Current
591}
592
593extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
594 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800595 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700596 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800597 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700598 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700599 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800600 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
601 if (LIKELY(field != NULL)) {
602 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700603 }
604 return 0; // Will throw exception by checking with Thread::Current
605}
606
607extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
608 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800609 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700610 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800611 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700612 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700613 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800614 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
615 if (LIKELY(field != NULL)) {
616 return field->GetObj(NULL);
617 }
618 return NULL; // Will throw exception by checking with Thread::Current
619}
620
621extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
622 const Method* referrer, Thread* self, Method** sp) {
623 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
624 if (LIKELY(field != NULL && obj != NULL)) {
625 return field->Get32(obj);
626 }
627 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
628 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
629 if (LIKELY(field != NULL)) {
630 if (UNLIKELY(obj == NULL)) {
631 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700632 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800633 return field->Get32(obj);
634 }
635 }
636 return 0; // Will throw exception by checking with Thread::Current
637}
638
639extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
640 const Method* referrer, Thread* self, Method** sp) {
641 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
642 if (LIKELY(field != NULL && obj != NULL)) {
643 return field->Get64(obj);
644 }
645 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
646 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
647 if (LIKELY(field != NULL)) {
648 if (UNLIKELY(obj == NULL)) {
649 ThrowNullPointerExceptionForFieldAccess(self, field, true);
650 } else {
651 return field->Get64(obj);
652 }
653 }
654 return 0; // Will throw exception by checking with Thread::Current
655}
656
657extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
658 const Method* referrer, Thread* self, Method** sp) {
659 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
660 if (LIKELY(field != NULL && obj != NULL)) {
661 return field->GetObj(obj);
662 }
663 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
664 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
665 if (LIKELY(field != NULL)) {
666 if (UNLIKELY(obj == NULL)) {
667 ThrowNullPointerExceptionForFieldAccess(self, field, true);
668 } else {
669 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700670 }
671 }
672 return NULL; // Will throw exception by checking with Thread::Current
673}
674
Ian Rogers1bddec32012-02-04 12:27:34 -0800675extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
676 const Method* referrer, Thread* self, Method** sp) {
677 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700678 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800679 field->Set32(NULL, new_value);
680 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700681 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700682 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800683 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
684 if (LIKELY(field != NULL)) {
685 field->Set32(NULL, new_value);
686 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700687 }
688 return -1; // failure
689}
690
691extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
692 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800693 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700694 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800695 field->Set64(NULL, new_value);
696 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700697 }
698 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800699 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700700 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800701 field->Set64(NULL, new_value);
702 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700703 }
704 return -1; // failure
705}
706
Ian Rogers1bddec32012-02-04 12:27:34 -0800707extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
708 const Method* referrer, Thread* self, Method** sp) {
709 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700710 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800711 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700712 field->SetObj(NULL, new_value);
713 return 0; // success
714 }
715 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700716 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800717 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
718 if (LIKELY(field != NULL)) {
719 field->SetObj(NULL, new_value);
720 return 0; // success
721 }
722 return -1; // failure
723}
724
725extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
726 const Method* referrer, Thread* self, Method** sp) {
727 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
728 if (LIKELY(field != NULL && obj != NULL)) {
729 field->Set32(obj, new_value);
730 return 0; // success
731 }
732 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
733 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
734 if (LIKELY(field != NULL)) {
735 if (UNLIKELY(obj == NULL)) {
736 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700737 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800738 field->Set32(obj, new_value);
739 return 0; // success
740 }
741 }
742 return -1; // failure
743}
744
745extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
746 Thread* self, Method** sp) {
747 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
748 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
749 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
750 if (LIKELY(field != NULL && obj != NULL)) {
751 field->Set64(obj, new_value);
752 return 0; // success
753 }
754 *sp = callee_save;
755 self->SetTopOfStack(sp, 0);
756 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
757 if (LIKELY(field != NULL)) {
758 if (UNLIKELY(obj == NULL)) {
759 ThrowNullPointerExceptionForFieldAccess(self, field, false);
760 } else {
761 field->Set64(obj, new_value);
762 return 0; // success
763 }
764 }
765 return -1; // failure
766}
767
768extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
769 const Method* referrer, Thread* self, Method** sp) {
770 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
771 if (LIKELY(field != NULL && obj != NULL)) {
772 field->SetObj(obj, new_value);
773 return 0; // success
774 }
775 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
776 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
777 if (LIKELY(field != NULL)) {
778 if (UNLIKELY(obj == NULL)) {
779 ThrowNullPointerExceptionForFieldAccess(self, field, false);
780 } else {
781 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700782 return 0; // success
783 }
784 }
785 return -1; // failure
786}
787
Shih-wei Liao2d831012011-09-28 22:06:53 -0700788// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
789// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800790// When verification/compiler hasn't been able to verify access, optionally perform an access
791// check.
792static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
793 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700794 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700795 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700796 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700797 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700798 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700799 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700800 return NULL; // Failure
801 }
802 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800803 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800804 if (UNLIKELY(!klass->IsInstantiable())) {
805 self->ThrowNewException("Ljava/lang/InstantiationError;",
806 PrettyDescriptor(klass).c_str());
807 return NULL; // Failure
808 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800809 Class* referrer = method->GetDeclaringClass();
810 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800811 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800812 return NULL; // Failure
813 }
814 }
buzbee33a129c2011-10-06 16:53:20 -0700815 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
816 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700817 return NULL; // Failure
818 }
819 return klass->AllocObject();
820}
821
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800822extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
823 Thread* self, Method** sp) {
824 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
825 return AllocObjectFromCode(type_idx, method, self, false);
826}
827
Ian Rogers28ad40d2011-10-27 15:19:26 -0700828extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
829 Thread* self, Method** sp) {
830 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800831 return AllocObjectFromCode(type_idx, method, self, true);
832}
833
834// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
835// it cannot be resolved, throw an error. If it can, use it to create an array.
836// When verification/compiler hasn't been able to verify access, optionally perform an access
837// check.
838static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
839 Thread* self, bool access_check) {
840 if (UNLIKELY(component_count < 0)) {
841 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
842 component_count);
843 return NULL; // Failure
844 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700845 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800846 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
847 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
848 if (klass == NULL) { // Error
849 DCHECK(Thread::Current()->IsExceptionPending());
850 return NULL; // Failure
851 }
852 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
853 }
854 if (access_check) {
855 Class* referrer = method->GetDeclaringClass();
856 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800857 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700858 return NULL; // Failure
859 }
860 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800861 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700862}
863
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800864extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
865 Thread* self, Method** sp) {
866 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
867 return AllocArrayFromCode(type_idx, method, component_count, self, false);
868}
869
870extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
871 int32_t component_count,
872 Thread* self, Method** sp) {
873 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
874 return AllocArrayFromCode(type_idx, method, component_count, self, true);
875}
876
877// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700878Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800879 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700880 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700881 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700882 return NULL; // Failure
883 }
884 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700885 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700886 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
887 if (klass == NULL) { // Error
888 DCHECK(Thread::Current()->IsExceptionPending());
889 return NULL; // Failure
890 }
891 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700892 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700893 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700894 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700895 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800896 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700897 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700898 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700899 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700901 }
902 return NULL; // Failure
903 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800904 if (access_check) {
905 Class* referrer = method->GetDeclaringClass();
906 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800907 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800908 return NULL; // Failure
909 }
910 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700911 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700912 return Array::Alloc(klass, component_count);
913 }
914}
915
Ian Rogersce9eca62011-10-07 17:11:03 -0700916extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
917 int32_t component_count, Thread* self, Method** sp) {
918 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800919 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700920}
921
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800922extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
923 int32_t component_count,
924 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700925 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800926 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700927}
928
Ian Rogerscaab8c42011-10-12 12:11:18 -0700929// Assignable test for code, won't throw. Null and equality tests already performed
930uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
931 DCHECK(klass != NULL);
932 DCHECK(ref_class != NULL);
933 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
934}
935
Shih-wei Liao2d831012011-09-28 22:06:53 -0700936// 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 -0700937extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700938 DCHECK(a->IsClass()) << PrettyClass(a);
939 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700940 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700941 return 0; // Success
942 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700943 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700944 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700945 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800946 PrettyDescriptor(a).c_str(),
947 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700948 return -1; // Failure
949 }
950}
951
952// Tests whether 'element' can be assigned into an array of type 'array_class'.
953// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700954extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
955 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700956 DCHECK(array_class != NULL);
957 // element can't be NULL as we catch this is screened in runtime_support
958 Class* element_class = element->GetClass();
959 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700960 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700961 return 0; // Success
962 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700963 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700964 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800965 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800966 PrettyDescriptor(element_class).c_str(),
967 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700968 return -1; // Failure
969 }
970}
971
Elliott Hughesf3778f62012-01-26 14:14:35 -0800972Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
973 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700974 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
975 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700976 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700977 CHECK(self->IsExceptionPending());
978 return NULL; // Failure - Indicate to caller to deliver exception
979 }
Elliott Hughesf3778f62012-01-26 14:14:35 -0800980 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800981 Class* referring_class = referrer->GetDeclaringClass();
982 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
983 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800984 return NULL; // Failure - Indicate to caller to deliver exception
985 }
986 // If we're just implementing const-class, we shouldn't call <clinit>.
987 if (!can_run_clinit) {
988 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -0700989 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700990 // If we are the <clinit> of this class, just return our storage.
991 //
992 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
993 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800994 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700995 return klass;
996 }
997 if (!class_linker->EnsureInitialized(klass, true)) {
998 CHECK(self->IsExceptionPending());
999 return NULL; // Failure - Indicate to caller to deliver exception
1000 }
1001 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
1002 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001003}
1004
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001005extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
1006 Thread* self, Method** sp) {
1007 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001008 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001009}
1010
Ian Rogers28ad40d2011-10-27 15:19:26 -07001011extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
1012 Method** sp) {
1013 // Called when method->dex_cache_resolved_types_[] misses
1014 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001015 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001016}
1017
Ian Rogersb093c6b2011-10-31 16:19:55 -07001018extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
1019 const Method* referrer, Thread* self,
1020 Method** sp) {
1021 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
1022 // unpopulated
1023 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001024 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -07001025}
1026
buzbee48d72222012-01-11 15:19:51 -08001027// Helper function to resolve virtual method
1028extern "C" Method* artResolveMethodFromCode(Method* referrer,
1029 uint32_t method_idx,
1030 bool is_direct,
1031 Thread* self,
1032 Method** sp) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001033 /*
1034 * Slow-path handler on invoke virtual method path in which
buzbee48d72222012-01-11 15:19:51 -08001035 * base method is unresolved at compile-time. Caller will
1036 * unwind if can't resolve.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001037 */
buzbee48d72222012-01-11 15:19:51 -08001038 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1039 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1040 Method* method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
buzbee48d72222012-01-11 15:19:51 -08001041 return method;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001042}
1043
Brian Carlstromaded5f72011-10-07 17:15:04 -07001044String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1045 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1046 return class_linker->ResolveString(string_idx, referrer);
1047}
1048
1049extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1050 Thread* self, Method** sp) {
1051 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1052 return ResolveStringFromCode(referrer, string_idx);
1053}
1054
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001055extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1056 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001057 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001058 // MonitorExit may throw exception
1059 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1060}
1061
1062extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1063 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1064 DCHECK(obj != NULL); // Assumed to have been checked before entry
1065 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001066 DCHECK(thread->HoldsLock(obj));
1067 // Only possible exception is NPE and is handled before entry
1068 DCHECK(!thread->IsExceptionPending());
1069}
1070
Ian Rogers4a510d82011-10-09 14:30:24 -07001071void CheckSuspendFromCode(Thread* thread) {
1072 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001073 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1074}
1075
Ian Rogers4a510d82011-10-09 14:30:24 -07001076extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1077 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001078 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001079 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1080}
1081
1082/*
1083 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1084 * not of sufficient length.
1085 *
1086 * NOTE: When dealing with a raw dex file, the data to be copied uses
1087 * little-endian ordering. Require that oat2dex do any required swapping
1088 * so this routine can get by with a memcpy().
1089 *
1090 * Format of the data:
1091 * ushort ident = 0x0300 magic value
1092 * ushort width width of each element in the table
1093 * uint size number of elements in the table
1094 * ubyte data[size*width] table of data values (may contain a single-byte
1095 * padding at the end)
1096 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001097extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1098 Thread* self, Method** sp) {
1099 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001100 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001101 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001102 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1103 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001104 return -1; // Error
1105 }
1106 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1107 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001108 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001109 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1110 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001111 return -1; // Error
1112 }
1113 uint16_t width = table[1];
1114 uint32_t size_in_bytes = size * width;
1115 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
1116 return 0; // Success
1117}
1118
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001119// Fast path method resolution that can't throw exceptions
1120static Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
1121 bool access_check, bool is_interface, bool is_super) {
1122 if (UNLIKELY(this_object == NULL)) {
1123 return NULL;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001124 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001125 Method* resolved_method =
1126 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
1127 if (UNLIKELY(resolved_method == NULL)) {
1128 return NULL;
1129 }
1130 if (access_check) {
1131 Class* methods_class = resolved_method->GetDeclaringClass();
1132 Class* referring_class = referrer->GetDeclaringClass();
1133 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1134 !referring_class->CanAccessMember(methods_class,
1135 resolved_method->GetAccessFlags()))) {
1136 // potential illegal access
1137 return NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -07001138 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001139 }
1140 if (is_interface) {
1141 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1142 } else if (is_super) {
1143 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1144 } else {
1145 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1146 }
1147}
1148
1149// Slow path method resolution
1150static Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
1151 Thread* self, bool access_check, bool is_interface,
1152 bool is_super) {
1153 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1154 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, false);
1155 if (LIKELY(resolved_method != NULL)) {
1156 if (!access_check) {
1157 if (is_interface) {
1158 Method* interface_method =
1159 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1160 if (UNLIKELY(interface_method == NULL)) {
1161 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1162 resolved_method,
1163 this_object);
1164 return NULL;
1165 } else {
1166 return interface_method;
1167 }
1168 } else {
1169 ObjectArray<Method>* vtable;
1170 uint16_t vtable_index = resolved_method->GetMethodIndex();
1171 if (is_super) {
1172 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
1173 } else {
1174 vtable = this_object->GetClass()->GetVTable();
1175 }
1176 // TODO: eliminate bounds check?
1177 return vtable->Get(vtable_index);
1178 }
1179 } else {
1180 Class* methods_class = resolved_method->GetDeclaringClass();
1181 Class* referring_class = referrer->GetDeclaringClass();
1182 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1183 !referring_class->CanAccessMember(methods_class,
1184 resolved_method->GetAccessFlags()))) {
1185 // The referring class can't access the resolved method, this may occur as a result of a
1186 // protected method being made public by implementing an interface that re-declares the
1187 // method public. Resort to the dex file to determine the correct class for the access check
1188 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
1189 methods_class = class_linker->ResolveType(dex_file,
1190 dex_file.GetMethodId(method_idx).class_idx_,
1191 referring_class);
1192 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
1193 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
1194 referrer, resolved_method, is_interface,
1195 is_super);
1196 return NULL; // failure
1197 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
1198 resolved_method->GetAccessFlags()))) {
1199 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
1200 return NULL; // failure
1201 }
1202 }
1203 if (is_interface) {
1204 Method* interface_method =
1205 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1206 if (UNLIKELY(interface_method == NULL)) {
1207 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1208 resolved_method,
1209 this_object);
1210 return NULL;
1211 } else {
1212 return interface_method;
1213 }
1214 } else {
1215 ObjectArray<Method>* vtable;
1216 uint16_t vtable_index = resolved_method->GetMethodIndex();
1217 if (is_super) {
1218 Class* super_class = referring_class->GetSuperClass();
1219 if (LIKELY(super_class != NULL)) {
1220 vtable = referring_class->GetSuperClass()->GetVTable();
1221 } else {
1222 vtable = NULL;
1223 }
1224 } else {
1225 vtable = this_object->GetClass()->GetVTable();
1226 }
1227 if (LIKELY(vtable != NULL &&
1228 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
1229 return vtable->GetWithoutChecks(vtable_index);
1230 } else {
1231 // Behavior to agree with that of the verifier
1232 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
1233 "attempt to invoke %s method '%s' from '%s'"
1234 " using incorrect form of method dispatch",
1235 (is_super ? "super class" : "virtual"),
1236 PrettyMethod(resolved_method).c_str(),
1237 PrettyMethod(referrer).c_str());
1238 return NULL;
1239 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001240 }
1241 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001242 }
1243 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
1244 return NULL;
1245}
1246
1247static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
1248 Thread* self, Method** sp, bool access_check, bool is_interface,
1249 bool is_super){
1250 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1251 is_interface, is_super);
1252 if (UNLIKELY(method == NULL)) {
1253 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1254 if (UNLIKELY(this_object == NULL)) {
1255 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, is_interface,
1256 is_super);
1257 return 0; // failure
1258 }
1259 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check,
1260 is_interface, is_super);
1261 if (UNLIKELY(method == NULL)) {
1262 CHECK(self->IsExceptionPending());
1263 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001264 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001265 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001266 // TODO: DCHECK
1267 CHECK(!self->IsExceptionPending());
1268 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001269
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001270 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001271 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1272 uint64_t result = ((code_uint << 32) | method_uint);
1273 return result;
1274}
1275
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001276// See comments in runtime_support_asm.S
1277extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1278 Method* caller_method, Thread* self,
1279 Method** sp) {
1280 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, true, false);
1281}
1282
1283extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1284 Object* this_object,
1285 Method* caller_method, Thread* self,
1286 Method** sp) {
1287 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, true, false);
1288}
1289
1290extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1291 Object* this_object,
1292 Method* caller_method, Thread* self,
1293 Method** sp) {
1294 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, false, true);
1295}
1296
1297extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(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, false, false);
1302}
1303
Ian Rogers466bb252011-10-14 03:29:56 -07001304static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1305 ScopedLocalRef<jclass> jlr_UTE_class(env,
1306 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1307 if (jlr_UTE_class.get() == NULL) {
1308 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1309 } else {
1310 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1311 "(Ljava/lang/Throwable;)V");
1312 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1313 ScopedLocalRef<jthrowable> jlr_UTE(env,
1314 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1315 jexception)));
1316 int rc = env->Throw(jlr_UTE.get());
1317 if (rc != JNI_OK) {
1318 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1319 }
1320 }
1321 CHECK(self->IsExceptionPending());
1322}
1323
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001324// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1325// which is responsible for recording callee save registers. We explicitly handlerize incoming
1326// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1327// the invocation handler which is a field within the proxy object receiver.
1328extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001329 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001330 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001331 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1332 DCHECK_EQ(*proxy_sp, proxy_method);
1333 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001334 // TODO: ARM specific
1335 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1336 // Start new JNI local reference state
1337 JNIEnvExt* env = self->GetJniEnv();
1338 ScopedJniEnvLocalRefState env_state(env);
1339 // Create local ref. copies of proxy method and the receiver
1340 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1341 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1342
Ian Rogers14b1b242011-10-11 18:54:34 -07001343 // Placing into local references incoming arguments from the caller's register arguments,
1344 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001345 MethodHelper proxy_mh(proxy_method);
1346 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001347 size_t args_in_regs = 0;
1348 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001349 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001350 if (args_in_regs > 2) {
1351 args_in_regs = 2;
1352 break;
1353 }
1354 }
1355 size_t cur_arg = 0; // current stack location to read
1356 size_t param_index = 1; // skip receiver
1357 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001358 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001359 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001360 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001361 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001362 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001363 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001364 param_index++;
1365 }
1366 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001367 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001368 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001369 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001370 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1371 jobject jobj = AddLocalReference<jobject>(env, obj);
1372 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001373 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001374 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001375 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001376 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001377 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1378 jvalue args_jobj[3];
1379 args_jobj[0].l = rcvr_jobj;
1380 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001381 // Args array, if no arguments then NULL (don't include receiver in argument count)
1382 args_jobj[2].l = NULL;
1383 ObjectArray<Object>* args = NULL;
1384 if ((num_params - 1) > 0) {
1385 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001386 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001387 CHECK(self->IsExceptionPending());
1388 return;
1389 }
1390 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1391 }
1392 // Convert proxy method into expected interface method
1393 Method* interface_method = proxy_method->FindOverriddenMethod();
1394 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001395 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001396 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001397 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001398 cur_arg = 0; // reset stack location to read to start
1399 // reset index, will index into param type array which doesn't include the receiver
1400 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001401 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001402 CHECK(param_types != NULL);
1403 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1404 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1405 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1406 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001407 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001408 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001409 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001410 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001411 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1412 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1413 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001414 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001415 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001416 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001417 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001418 if (self->IsExceptionPending()) {
1419 return;
1420 }
1421 obj = val.l;
1422 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001423 args->Set(param_index, obj);
1424 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1425 param_index++;
1426 }
1427 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001428 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1429 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001430 Class* param_type = param_types->Get(param_index);
1431 Object* obj;
1432 if (!param_type->IsPrimitive()) {
1433 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1434 } else {
1435 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001436 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001437 if (self->IsExceptionPending()) {
1438 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001439 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001440 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001441 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001442 args->Set(param_index, obj);
1443 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1444 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001445 }
1446 // Get the InvocationHandler method and the field that holds it within the Proxy object
1447 static jmethodID inv_hand_invoke_mid = NULL;
1448 static jfieldID proxy_inv_hand_fid = NULL;
1449 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001450 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001451 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001452 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001453 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1454 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1455 }
Ian Rogers466bb252011-10-14 03:29:56 -07001456 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001457 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1458 // Call InvocationHandler.invoke
1459 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1460 // Place result in stack args
1461 if (!self->IsExceptionPending()) {
1462 Object* result_ref = self->DecodeJObject(result);
1463 if (result_ref != NULL) {
1464 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001465 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001466 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1467 } else {
1468 *reinterpret_cast<jobject*>(stack_args) = NULL;
1469 }
Ian Rogers466bb252011-10-14 03:29:56 -07001470 } else {
1471 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1472 // a UndeclaredThrowableException.
1473 Throwable* exception = self->GetException();
1474 self->ClearException();
1475 if (!exception->IsCheckedException()) {
1476 self->SetException(exception);
1477 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001478 SynthesizedProxyClass* proxy_class =
1479 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1480 int throws_index = -1;
1481 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1482 for (size_t i = 0; i < num_virt_methods; i++) {
1483 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1484 throws_index = i;
1485 break;
1486 }
1487 }
1488 CHECK_NE(throws_index, -1);
1489 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001490 Class* exception_class = exception->GetClass();
1491 bool declares_exception = false;
1492 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1493 Class* declared_exception = declared_exceptions->Get(i);
1494 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1495 }
1496 if (declares_exception) {
1497 self->SetException(exception);
1498 } else {
1499 ThrowNewUndeclaredThrowableException(self, env, exception);
1500 }
1501 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001502 }
1503}
1504
jeffhaoe343b762011-12-05 16:36:44 -08001505extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001506 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001507 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1508 self->PushTraceStackFrame(trace_frame);
1509
jeffhao2692b572011-12-16 15:42:28 -08001510 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001511
jeffhao2692b572011-12-16 15:42:28 -08001512 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001513}
1514
1515extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001516 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001517 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1518 Method* method = trace_frame.method_;
1519 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001520
jeffhao2692b572011-12-16 15:42:28 -08001521 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001522
1523 return lr;
1524}
1525
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001526uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001527 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001528 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1529 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001530 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001531
jeffhao2692b572011-12-16 15:42:28 -08001532 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001533
1534 return lr;
1535}
1536
Shih-wei Liao2d831012011-09-28 22:06:53 -07001537/*
1538 * Float/double conversion requires clamping to min and max of integer form. If
1539 * target doesn't support this normally, use these.
1540 */
1541int64_t D2L(double d) {
1542 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1543 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1544 if (d >= kMaxLong)
1545 return (int64_t)0x7fffffffffffffffULL;
1546 else if (d <= kMinLong)
1547 return (int64_t)0x8000000000000000ULL;
1548 else if (d != d) // NaN case
1549 return 0;
1550 else
1551 return (int64_t)d;
1552}
1553
1554int64_t F2L(float f) {
1555 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1556 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1557 if (f >= kMaxLong)
1558 return (int64_t)0x7fffffffffffffffULL;
1559 else if (f <= kMinLong)
1560 return (int64_t)0x8000000000000000ULL;
1561 else if (f != f) // NaN case
1562 return 0;
1563 else
1564 return (int64_t)f;
1565}
1566
1567} // namespace art