blob: eb06df5ef86ccb21d942d89973caa6172dec3e72 [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
122// Return value helper for jobject return types
123extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -0700124 if (thread->IsExceptionPending()) {
125 return NULL;
126 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700127 return thread->DecodeJObject(obj);
128}
129
130extern void* FindNativeMethod(Thread* thread) {
131 DCHECK(Thread::Current() == thread);
132
133 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
134 DCHECK(method != NULL);
135
136 // Lookup symbol address for method, on failure we'll return NULL with an
137 // exception set, otherwise we return the address of the method we found.
138 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
139 if (native_code == NULL) {
140 DCHECK(thread->IsExceptionPending());
141 return NULL;
142 } else {
143 // Register so that future calls don't come here
144 method->RegisterNative(native_code);
145 return native_code;
146 }
147}
148
149// Called by generated call to throw an exception
150extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
151 /*
152 * exception may be NULL, in which case this routine should
153 * throw NPE. NOTE: this is a convenience for generated code,
154 * which previously did the null check inline and constructed
155 * and threw a NPE if NULL. This routine responsible for setting
156 * exception_ in thread and delivering the exception.
157 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700158 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700159 if (exception == NULL) {
160 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
161 } else {
162 thread->SetException(exception);
163 }
164 thread->DeliverException();
165}
166
167// Deliver an exception that's pending on thread helping set up a callee save frame on the way
168extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700169 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700170 thread->DeliverException();
171}
172
173// Called by generated call to throw a NPE exception
174extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700175 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700176 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700177 thread->DeliverException();
178}
179
180// Called by generated call to throw an arithmetic divide by zero exception
181extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700182 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700183 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
184 thread->DeliverException();
185}
186
187// Called by generated call to throw an arithmetic divide by zero exception
188extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700189 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
190 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
191 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700192 thread->DeliverException();
193}
194
195// Called by the AbstractMethodError stub (not runtime support)
196extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700197 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
198 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
199 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700200 thread->DeliverException();
201}
202
203extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700204 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800205 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800206 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800207 artTraceMethodUnwindFromCode(thread);
208 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700209 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700210 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
211 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700212 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700213 thread->ResetDefaultStackEnd(); // Return to default stack size
214 thread->DeliverException();
215}
216
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700217static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700218 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700219 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
220 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
221
222 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700223 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700224 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
225 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700226 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700227 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
228 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700229 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700230 type_idx = ref;
231 } else {
232 CHECK(false) << static_cast<int>(ref_type);
233 }
234
Ian Rogers0571d352011-11-03 19:51:38 -0700235 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700236 if (!access) {
237 return class_name;
238 }
239
240 std::string result;
241 result += "tried to access class ";
242 result += class_name;
243 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700245 return result;
246}
247
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700248static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700249 verifier::VerifyErrorRefType ref_type, bool access) {
250 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700251
252 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
253 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
254
255 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700256 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700257 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700258 if (!access) {
259 return class_name + "." + field_name;
260 }
261
262 std::string result;
263 result += "tried to access field ";
264 result += class_name + "." + field_name;
265 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800266 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700267 return result;
268}
269
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700270static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700271 verifier::VerifyErrorRefType ref_type, bool access) {
272 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700273
274 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
275 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
276
277 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700278 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700279 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700280 if (!access) {
281 return class_name + "." + method_name;
282 }
283
284 std::string result;
285 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700286 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700287 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700288 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700290 return result;
291}
292
293extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700294 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
295 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700296 frame.Next();
297 Method* method = frame.GetMethod();
298
Ian Rogersd81871c2011-10-03 13:57:23 -0700299 verifier::VerifyErrorRefType ref_type =
300 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700301
302 const char* exception_class = "Ljava/lang/VerifyError;";
303 std::string msg;
304
Ian Rogersd81871c2011-10-03 13:57:23 -0700305 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
306 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700307 exception_class = "Ljava/lang/NoClassDefFoundError;";
308 msg = ClassNameFromIndex(method, ref, ref_type, false);
309 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700310 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700311 exception_class = "Ljava/lang/NoSuchFieldError;";
312 msg = FieldNameFromIndex(method, ref, ref_type, false);
313 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700314 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700315 exception_class = "Ljava/lang/NoSuchMethodError;";
316 msg = MethodNameFromIndex(method, ref, ref_type, false);
317 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700318 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700319 exception_class = "Ljava/lang/IllegalAccessError;";
320 msg = ClassNameFromIndex(method, ref, ref_type, true);
321 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700323 exception_class = "Ljava/lang/IllegalAccessError;";
324 msg = FieldNameFromIndex(method, ref, ref_type, true);
325 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700326 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700327 exception_class = "Ljava/lang/IllegalAccessError;";
328 msg = MethodNameFromIndex(method, ref, ref_type, true);
329 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700330 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700331 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
332 msg = ClassNameFromIndex(method, ref, ref_type, false);
333 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700334 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700335 exception_class = "Ljava/lang/InstantiationError;";
336 msg = ClassNameFromIndex(method, ref, ref_type, false);
337 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700338 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700339 // Generic VerifyError; use default exception, no message.
340 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700341 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700342 CHECK(false);
343 break;
344 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700345 self->ThrowNewException(exception_class, msg.c_str());
346 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700347}
348
349extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700350 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700351 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700352 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700353 thread->DeliverException();
354}
355
356extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700357 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700358 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700359 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700360 thread->DeliverException();
361}
362
Elliott Hughese1410a22011-10-04 12:10:24 -0700363extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700364 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
365 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700366 frame.Next();
367 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700368 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700369 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700370 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700371}
372
373extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700374 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700375 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700376 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700377 thread->DeliverException();
378}
379
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700380void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700381 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700382 // TODO: this code is specific to ARM
383 // On entry the stack pointed by sp is:
384 // | argN | |
385 // | ... | |
386 // | arg4 | |
387 // | arg3 spill | | Caller's frame
388 // | arg2 spill | |
389 // | arg1 spill | |
390 // | Method* | ---
391 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700392 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700393 // | R3 | arg3
394 // | R2 | arg2
395 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700396 // | R0 |
397 // | Method* | <- sp
398 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
399 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
400 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
401 uintptr_t caller_pc = regs[10];
402 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700403 // Start new JNI local reference state
404 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700405 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700406 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700407 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700408 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
409 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700410 size_t args_in_regs = 0;
411 for (size_t i = 1; i < shorty_len; i++) {
412 char c = shorty[i];
413 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
414 if (args_in_regs > 3) {
415 args_in_regs = 3;
416 break;
417 }
418 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700419 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700420 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700421 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700422 // less two as return address may span into next dex instruction
423 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800424 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700425 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
426 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700427 Instruction::Code instr_code = instr->Opcode();
428 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
429 (instr_code == Instruction::INVOKE_STATIC_RANGE);
430 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
431 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700432 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700433 is_static = type == Runtime::kStaticMethod;
434 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700435 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700436 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700437 if (!is_static) {
438 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
439 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700440 if (args_in_regs < 3) {
441 // If we thought we had fewer than 3 arguments in registers, account for the receiver
442 args_in_regs++;
443 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700444 AddLocalReference<jobject>(env, obj);
445 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700446 size_t shorty_index = 1; // skip return value
447 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
448 // R0)
449 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
450 char c = shorty[shorty_index];
451 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700452 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700453 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
454 AddLocalReference<jobject>(env, obj);
455 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700456 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
457 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700458 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700459 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700460 while (shorty_index < shorty_len) {
461 char c = shorty[shorty_index];
462 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700463 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700464 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700465 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700466 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700467 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700468 }
469 // Resolve method filling in dex cache
470 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700471 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800472 if (LIKELY(called->IsDirect())) {
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800473 // Ensure that the called method's class is initialized
474 Class* called_class = called->GetDeclaringClass();
475 linker->EnsureInitialized(called_class, true);
476 if (LIKELY(called_class->IsInitialized())) {
477 // Update CodeAndDirectMethod table and avoid the trampoline when we know the called class
478 // is initialized (see test 084-class-init SlowInit)
479 Method* caller = *caller_sp;
480 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
481 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
482 // We got this far, ensure that the declaring class is initialized
483 linker->EnsureInitialized(called->GetDeclaringClass(), true);
484 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800485 } else {
486 // Direct method has been made virtual
487 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
488 "Expected direct method but found virtual: %s",
489 PrettyMethod(called, true).c_str());
490 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700491 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700492 void* code;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700493 if (UNLIKELY(thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700494 // Something went wrong in ResolveMethod or EnsureInitialized,
495 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700496 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700497 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700498 thread->ClearException();
499 } else {
500 // Expect class to at least be initializing
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800501 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogersad25ac52011-10-04 19:13:33 -0700502 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700503 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700504 code = const_cast<void*>(called->GetCode());
505 }
506 return code;
507}
508
Ian Rogerscaab8c42011-10-12 12:11:18 -0700509// Fast path field resolution that can't throw exceptions
Ian Rogers1bddec32012-02-04 12:27:34 -0800510static Field* FindFieldFast(uint32_t field_idx, const Method* referrer, bool is_primitive,
511 size_t expected_size) {
Ian Rogers53a77a52012-02-06 09:47:45 -0800512 Field* resolved_field = referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700513 if (UNLIKELY(resolved_field == NULL)) {
514 return NULL;
515 }
516 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800517 // Check class is initiliazed or initializing
Ian Rogerscaab8c42011-10-12 12:11:18 -0700518 if (UNLIKELY(!fields_class->IsInitializing())) {
519 return NULL;
520 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800521 Class* referring_class = referrer->GetDeclaringClass();
522 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
523 !referring_class->CanAccessMember(fields_class,
524 resolved_field->GetAccessFlags()))) {
525 // illegal access
526 return NULL;
527 }
528 FieldHelper fh(resolved_field);
529 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
530 fh.FieldSize() != expected_size)) {
531 return NULL;
532 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700533 return resolved_field;
534}
535
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800536
Ian Rogerscaab8c42011-10-12 12:11:18 -0700537// Slow path field resolution and declaring class initialization
Ian Rogers1bddec32012-02-04 12:27:34 -0800538Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self,
539 bool is_static, bool is_primitive, size_t expected_size) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700540 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700541 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
542 if (LIKELY(resolved_field != NULL)) {
543 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers1bddec32012-02-04 12:27:34 -0800544 Class* referring_class = referrer->GetDeclaringClass();
545 if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800546 ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class);
Ian Rogers1bddec32012-02-04 12:27:34 -0800547 } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
548 resolved_field->GetAccessFlags()))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800549 ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field);
Ian Rogers1bddec32012-02-04 12:27:34 -0800550 return NULL; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -0700551 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800552 FieldHelper fh(resolved_field);
553 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
554 fh.FieldSize() != expected_size)) {
555 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Elliott Hughes1e409252012-02-06 11:21:27 -0800556 "Attempted read of %zd-bit %s on field '%s'",
Ian Rogers1bddec32012-02-04 12:27:34 -0800557 expected_size * (32 / sizeof(int32_t)),
558 is_primitive ? "primitive" : "non-primitive",
559 PrettyField(resolved_field, true).c_str());
560 }
561 if (!is_static) {
562 // instance fields must be being accessed on an initialized class
Ian Rogerscaab8c42011-10-12 12:11:18 -0700563 return resolved_field;
Ian Rogers1bddec32012-02-04 12:27:34 -0800564 } else {
565 // If the class is already initializing, we must be inside <clinit>, or
566 // we'd still be waiting for the lock.
567 if (fields_class->IsInitializing()) {
568 return resolved_field;
569 } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
570 return resolved_field;
571 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700572 }
573 }
Ian Rogers1bddec32012-02-04 12:27:34 -0800574 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
Ian Rogersce9eca62011-10-07 17:11:03 -0700575 return NULL;
576}
577
Ian Rogersce9eca62011-10-07 17:11:03 -0700578extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
579 Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800580 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700581 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800582 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700583 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700584 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800585 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
586 if (LIKELY(field != NULL)) {
587 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700588 }
589 return 0; // Will throw exception by checking with Thread::Current
590}
591
592extern "C" uint64_t artGet64StaticFromCode(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(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700595 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800596 return field->Get64(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(int64_t));
600 if (LIKELY(field != NULL)) {
601 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700602 }
603 return 0; // Will throw exception by checking with Thread::Current
604}
605
606extern "C" Object* artGetObjStaticFromCode(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, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700609 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800610 return field->GetObj(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, false, sizeof(Object*));
614 if (LIKELY(field != NULL)) {
615 return field->GetObj(NULL);
616 }
617 return NULL; // Will throw exception by checking with Thread::Current
618}
619
620extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
621 const Method* referrer, Thread* self, Method** sp) {
622 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
623 if (LIKELY(field != NULL && obj != NULL)) {
624 return field->Get32(obj);
625 }
626 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
627 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
628 if (LIKELY(field != NULL)) {
629 if (UNLIKELY(obj == NULL)) {
630 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700631 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800632 return field->Get32(obj);
633 }
634 }
635 return 0; // Will throw exception by checking with Thread::Current
636}
637
638extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
639 const Method* referrer, Thread* self, Method** sp) {
640 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
641 if (LIKELY(field != NULL && obj != NULL)) {
642 return field->Get64(obj);
643 }
644 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
645 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
646 if (LIKELY(field != NULL)) {
647 if (UNLIKELY(obj == NULL)) {
648 ThrowNullPointerExceptionForFieldAccess(self, field, true);
649 } else {
650 return field->Get64(obj);
651 }
652 }
653 return 0; // Will throw exception by checking with Thread::Current
654}
655
656extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
657 const Method* referrer, Thread* self, Method** sp) {
658 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
659 if (LIKELY(field != NULL && obj != NULL)) {
660 return field->GetObj(obj);
661 }
662 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
663 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
664 if (LIKELY(field != NULL)) {
665 if (UNLIKELY(obj == NULL)) {
666 ThrowNullPointerExceptionForFieldAccess(self, field, true);
667 } else {
668 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700669 }
670 }
671 return NULL; // Will throw exception by checking with Thread::Current
672}
673
Ian Rogers1bddec32012-02-04 12:27:34 -0800674extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
675 const Method* referrer, Thread* self, Method** sp) {
676 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700677 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800678 field->Set32(NULL, new_value);
679 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700680 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700681 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800682 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int32_t));
683 if (LIKELY(field != NULL)) {
684 field->Set32(NULL, new_value);
685 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700686 }
687 return -1; // failure
688}
689
690extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
691 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800692 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700693 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800694 field->Set64(NULL, new_value);
695 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700696 }
697 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800698 field = FindFieldFromCode(field_idx, referrer, self, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700699 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800700 field->Set64(NULL, new_value);
701 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700702 }
703 return -1; // failure
704}
705
Ian Rogers1bddec32012-02-04 12:27:34 -0800706extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
707 const Method* referrer, Thread* self, Method** sp) {
708 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700709 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800710 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700711 field->SetObj(NULL, new_value);
712 return 0; // success
713 }
714 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700715 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers1bddec32012-02-04 12:27:34 -0800716 field = FindFieldFromCode(field_idx, referrer, self, true, false, sizeof(Object*));
717 if (LIKELY(field != NULL)) {
718 field->SetObj(NULL, new_value);
719 return 0; // success
720 }
721 return -1; // failure
722}
723
724extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
725 const Method* referrer, Thread* self, Method** sp) {
726 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int32_t));
727 if (LIKELY(field != NULL && obj != NULL)) {
728 field->Set32(obj, new_value);
729 return 0; // success
730 }
731 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
732 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int32_t));
733 if (LIKELY(field != NULL)) {
734 if (UNLIKELY(obj == NULL)) {
735 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700736 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800737 field->Set32(obj, new_value);
738 return 0; // success
739 }
740 }
741 return -1; // failure
742}
743
744extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
745 Thread* self, Method** sp) {
746 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
747 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
748 Field* field = FindFieldFast(field_idx, referrer, true, sizeof(int64_t));
749 if (LIKELY(field != NULL && obj != NULL)) {
750 field->Set64(obj, new_value);
751 return 0; // success
752 }
753 *sp = callee_save;
754 self->SetTopOfStack(sp, 0);
755 field = FindFieldFromCode(field_idx, referrer, self, false, true, sizeof(int64_t));
756 if (LIKELY(field != NULL)) {
757 if (UNLIKELY(obj == NULL)) {
758 ThrowNullPointerExceptionForFieldAccess(self, field, false);
759 } else {
760 field->Set64(obj, new_value);
761 return 0; // success
762 }
763 }
764 return -1; // failure
765}
766
767extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
768 const Method* referrer, Thread* self, Method** sp) {
769 Field* field = FindFieldFast(field_idx, referrer, false, sizeof(Object*));
770 if (LIKELY(field != NULL && obj != NULL)) {
771 field->SetObj(obj, new_value);
772 return 0; // success
773 }
774 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
775 field = FindFieldFromCode(field_idx, referrer, self, false, false, sizeof(Object*));
776 if (LIKELY(field != NULL)) {
777 if (UNLIKELY(obj == NULL)) {
778 ThrowNullPointerExceptionForFieldAccess(self, field, false);
779 } else {
780 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700781 return 0; // success
782 }
783 }
784 return -1; // failure
785}
786
Shih-wei Liao2d831012011-09-28 22:06:53 -0700787// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
788// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800789// When verification/compiler hasn't been able to verify access, optionally perform an access
790// check.
791static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
792 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700793 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700794 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700795 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700796 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700797 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700798 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700799 return NULL; // Failure
800 }
801 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800802 if (access_check) {
Ian Rogersd4135902012-02-03 18:05:08 -0800803 if (UNLIKELY(!klass->IsInstantiable())) {
804 self->ThrowNewException("Ljava/lang/InstantiationError;",
805 PrettyDescriptor(klass).c_str());
806 return NULL; // Failure
807 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800808 Class* referrer = method->GetDeclaringClass();
809 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800810 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800811 return NULL; // Failure
812 }
813 }
buzbee33a129c2011-10-06 16:53:20 -0700814 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
815 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700816 return NULL; // Failure
817 }
818 return klass->AllocObject();
819}
820
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800821extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
822 Thread* self, Method** sp) {
823 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
824 return AllocObjectFromCode(type_idx, method, self, false);
825}
826
Ian Rogers28ad40d2011-10-27 15:19:26 -0700827extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
828 Thread* self, Method** sp) {
829 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800830 return AllocObjectFromCode(type_idx, method, self, true);
831}
832
833// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
834// it cannot be resolved, throw an error. If it can, use it to create an array.
835// When verification/compiler hasn't been able to verify access, optionally perform an access
836// check.
837static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
838 Thread* self, bool access_check) {
839 if (UNLIKELY(component_count < 0)) {
840 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
841 component_count);
842 return NULL; // Failure
843 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700844 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800845 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
846 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
847 if (klass == NULL) { // Error
848 DCHECK(Thread::Current()->IsExceptionPending());
849 return NULL; // Failure
850 }
851 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
852 }
853 if (access_check) {
854 Class* referrer = method->GetDeclaringClass();
855 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800856 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700857 return NULL; // Failure
858 }
859 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800860 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700861}
862
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800863extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
864 Thread* self, Method** sp) {
865 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
866 return AllocArrayFromCode(type_idx, method, component_count, self, false);
867}
868
869extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
870 int32_t component_count,
871 Thread* self, Method** sp) {
872 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
873 return AllocArrayFromCode(type_idx, method, component_count, self, true);
874}
875
876// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700877Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800878 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700879 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700880 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700881 return NULL; // Failure
882 }
883 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700884 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700885 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
886 if (klass == NULL) { // Error
887 DCHECK(Thread::Current()->IsExceptionPending());
888 return NULL; // Failure
889 }
890 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700891 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700892 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700893 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700894 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800895 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700896 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700897 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700898 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800899 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700900 }
901 return NULL; // Failure
902 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800903 if (access_check) {
904 Class* referrer = method->GetDeclaringClass();
905 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800906 ThrowNewIllegalAccessErrorClass(self, referrer, klass);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800907 return NULL; // Failure
908 }
909 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700910 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700911 return Array::Alloc(klass, component_count);
912 }
913}
914
Ian Rogersce9eca62011-10-07 17:11:03 -0700915extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
916 int32_t component_count, Thread* self, Method** sp) {
917 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800918 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700919}
920
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800921extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
922 int32_t component_count,
923 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700924 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800925 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700926}
927
Ian Rogerscaab8c42011-10-12 12:11:18 -0700928// Assignable test for code, won't throw. Null and equality tests already performed
929uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
930 DCHECK(klass != NULL);
931 DCHECK(ref_class != NULL);
932 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
933}
934
Shih-wei Liao2d831012011-09-28 22:06:53 -0700935// 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 -0700936extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700937 DCHECK(a->IsClass()) << PrettyClass(a);
938 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700939 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700940 return 0; // Success
941 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700942 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700943 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700944 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800945 PrettyDescriptor(a).c_str(),
946 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700947 return -1; // Failure
948 }
949}
950
951// Tests whether 'element' can be assigned into an array of type 'array_class'.
952// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700953extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
954 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700955 DCHECK(array_class != NULL);
956 // element can't be NULL as we catch this is screened in runtime_support
957 Class* element_class = element->GetClass();
958 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700959 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700960 return 0; // Success
961 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700962 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700963 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800964 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800965 PrettyDescriptor(element_class).c_str(),
966 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700967 return -1; // Failure
968 }
969}
970
Elliott Hughesf3778f62012-01-26 14:14:35 -0800971Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
972 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700973 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
974 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700975 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700976 CHECK(self->IsExceptionPending());
977 return NULL; // Failure - Indicate to caller to deliver exception
978 }
Elliott Hughesf3778f62012-01-26 14:14:35 -0800979 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800980 Class* referring_class = referrer->GetDeclaringClass();
981 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
982 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800983 return NULL; // Failure - Indicate to caller to deliver exception
984 }
985 // If we're just implementing const-class, we shouldn't call <clinit>.
986 if (!can_run_clinit) {
987 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -0700988 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700989 // If we are the <clinit> of this class, just return our storage.
990 //
991 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
992 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800993 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700994 return klass;
995 }
996 if (!class_linker->EnsureInitialized(klass, true)) {
997 CHECK(self->IsExceptionPending());
998 return NULL; // Failure - Indicate to caller to deliver exception
999 }
1000 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
1001 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001002}
1003
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001004extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
1005 Thread* self, Method** sp) {
1006 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001007 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001008}
1009
Ian Rogers28ad40d2011-10-27 15:19:26 -07001010extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
1011 Method** sp) {
1012 // Called when method->dex_cache_resolved_types_[] misses
1013 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001014 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001015}
1016
Ian Rogersb093c6b2011-10-31 16:19:55 -07001017extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
1018 const Method* referrer, Thread* self,
1019 Method** sp) {
1020 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
1021 // unpopulated
1022 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -08001023 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -07001024}
1025
buzbee48d72222012-01-11 15:19:51 -08001026// Helper function to resolve virtual method
1027extern "C" Method* artResolveMethodFromCode(Method* referrer,
1028 uint32_t method_idx,
1029 bool is_direct,
1030 Thread* self,
1031 Method** sp) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001032 /*
1033 * Slow-path handler on invoke virtual method path in which
buzbee48d72222012-01-11 15:19:51 -08001034 * base method is unresolved at compile-time. Caller will
1035 * unwind if can't resolve.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001036 */
buzbee48d72222012-01-11 15:19:51 -08001037 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1038 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1039 Method* method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
buzbee48d72222012-01-11 15:19:51 -08001040 return method;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001041}
1042
Brian Carlstromaded5f72011-10-07 17:15:04 -07001043String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
1044 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1045 return class_linker->ResolveString(string_idx, referrer);
1046}
1047
1048extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
1049 Thread* self, Method** sp) {
1050 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
1051 return ResolveStringFromCode(referrer, string_idx);
1052}
1053
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001054extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
1055 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001056 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001057 // MonitorExit may throw exception
1058 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
1059}
1060
1061extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
1062 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
1063 DCHECK(obj != NULL); // Assumed to have been checked before entry
1064 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -07001065 DCHECK(thread->HoldsLock(obj));
1066 // Only possible exception is NPE and is handled before entry
1067 DCHECK(!thread->IsExceptionPending());
1068}
1069
Ian Rogers4a510d82011-10-09 14:30:24 -07001070void CheckSuspendFromCode(Thread* thread) {
1071 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001072 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1073}
1074
Ian Rogers4a510d82011-10-09 14:30:24 -07001075extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
1076 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001077 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001078 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
1079}
1080
1081/*
1082 * Fill the array with predefined constant values, throwing exceptions if the array is null or
1083 * not of sufficient length.
1084 *
1085 * NOTE: When dealing with a raw dex file, the data to be copied uses
1086 * little-endian ordering. Require that oat2dex do any required swapping
1087 * so this routine can get by with a memcpy().
1088 *
1089 * Format of the data:
1090 * ushort ident = 0x0300 magic value
1091 * ushort width width of each element in the table
1092 * uint size number of elements in the table
1093 * ubyte data[size*width] table of data values (may contain a single-byte
1094 * padding at the end)
1095 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001096extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
1097 Thread* self, Method** sp) {
1098 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001099 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001100 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001101 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
1102 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -07001103 return -1; // Error
1104 }
1105 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
1106 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001107 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001108 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
1109 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001110 return -1; // Error
1111 }
1112 uint16_t width = table[1];
1113 uint32_t size_in_bytes = size * width;
1114 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
1115 return 0; // Success
1116}
1117
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001118// Fast path method resolution that can't throw exceptions
1119static Method* FindMethodFast(uint32_t method_idx, Object* this_object, const Method* referrer,
1120 bool access_check, bool is_interface, bool is_super) {
1121 if (UNLIKELY(this_object == NULL)) {
1122 return NULL;
Shih-wei Liao2d831012011-09-28 22:06:53 -07001123 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001124 Method* resolved_method =
1125 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
1126 if (UNLIKELY(resolved_method == NULL)) {
1127 return NULL;
1128 }
1129 if (access_check) {
1130 Class* methods_class = resolved_method->GetDeclaringClass();
1131 Class* referring_class = referrer->GetDeclaringClass();
1132 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1133 !referring_class->CanAccessMember(methods_class,
1134 resolved_method->GetAccessFlags()))) {
1135 // potential illegal access
1136 return NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -07001137 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001138 }
1139 if (is_interface) {
1140 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1141 } else if (is_super) {
1142 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1143 } else {
1144 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
1145 }
1146}
1147
1148// Slow path method resolution
1149static Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer,
1150 Thread* self, bool access_check, bool is_interface,
1151 bool is_super) {
1152 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1153 Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, false);
1154 if (LIKELY(resolved_method != NULL)) {
1155 if (!access_check) {
1156 if (is_interface) {
1157 Method* interface_method =
1158 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1159 if (UNLIKELY(interface_method == NULL)) {
1160 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1161 resolved_method,
1162 this_object);
1163 return NULL;
1164 } else {
1165 return interface_method;
1166 }
1167 } else {
1168 ObjectArray<Method>* vtable;
1169 uint16_t vtable_index = resolved_method->GetMethodIndex();
1170 if (is_super) {
1171 vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
1172 } else {
1173 vtable = this_object->GetClass()->GetVTable();
1174 }
1175 // TODO: eliminate bounds check?
1176 return vtable->Get(vtable_index);
1177 }
1178 } else {
1179 Class* methods_class = resolved_method->GetDeclaringClass();
1180 Class* referring_class = referrer->GetDeclaringClass();
1181 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
1182 !referring_class->CanAccessMember(methods_class,
1183 resolved_method->GetAccessFlags()))) {
1184 // The referring class can't access the resolved method, this may occur as a result of a
1185 // protected method being made public by implementing an interface that re-declares the
1186 // method public. Resort to the dex file to determine the correct class for the access check
1187 const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache());
1188 methods_class = class_linker->ResolveType(dex_file,
1189 dex_file.GetMethodId(method_idx).class_idx_,
1190 referring_class);
1191 if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
1192 ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class,
1193 referrer, resolved_method, is_interface,
1194 is_super);
1195 return NULL; // failure
1196 } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
1197 resolved_method->GetAccessFlags()))) {
1198 ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method);
1199 return NULL; // failure
1200 }
1201 }
1202 if (is_interface) {
1203 Method* interface_method =
1204 this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
1205 if (UNLIKELY(interface_method == NULL)) {
1206 ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, referrer,
1207 resolved_method,
1208 this_object);
1209 return NULL;
1210 } else {
1211 return interface_method;
1212 }
1213 } else {
1214 ObjectArray<Method>* vtable;
1215 uint16_t vtable_index = resolved_method->GetMethodIndex();
1216 if (is_super) {
1217 Class* super_class = referring_class->GetSuperClass();
1218 if (LIKELY(super_class != NULL)) {
1219 vtable = referring_class->GetSuperClass()->GetVTable();
1220 } else {
1221 vtable = NULL;
1222 }
1223 } else {
1224 vtable = this_object->GetClass()->GetVTable();
1225 }
1226 if (LIKELY(vtable != NULL &&
1227 vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
1228 return vtable->GetWithoutChecks(vtable_index);
1229 } else {
1230 // Behavior to agree with that of the verifier
1231 self->ThrowNewExceptionF("Ljava/lang/NoSuchMethodError;",
1232 "attempt to invoke %s method '%s' from '%s'"
1233 " using incorrect form of method dispatch",
1234 (is_super ? "super class" : "virtual"),
1235 PrettyMethod(resolved_method).c_str(),
1236 PrettyMethod(referrer).c_str());
1237 return NULL;
1238 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001239 }
1240 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001241 }
1242 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
1243 return NULL;
1244}
1245
1246static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
1247 Thread* self, Method** sp, bool access_check, bool is_interface,
1248 bool is_super){
1249 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1250 is_interface, is_super);
1251 if (UNLIKELY(method == NULL)) {
1252 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1253 if (UNLIKELY(this_object == NULL)) {
1254 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, is_interface,
1255 is_super);
1256 return 0; // failure
1257 }
1258 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check,
1259 is_interface, is_super);
1260 if (UNLIKELY(method == NULL)) {
1261 CHECK(self->IsExceptionPending());
1262 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001263 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001264 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001265 // TODO: DCHECK
1266 CHECK(!self->IsExceptionPending());
1267 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001268
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001269 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001270 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1271 uint64_t result = ((code_uint << 32) | method_uint);
1272 return result;
1273}
1274
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001275// See comments in runtime_support_asm.S
1276extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1277 Method* caller_method, Thread* self,
1278 Method** sp) {
1279 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, true, false);
1280}
1281
1282extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1283 Object* this_object,
1284 Method* caller_method, Thread* self,
1285 Method** sp) {
1286 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, true, false);
1287}
1288
1289extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1290 Object* this_object,
1291 Method* caller_method, Thread* self,
1292 Method** sp) {
1293 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, false, true);
1294}
1295
1296extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1297 Object* this_object,
1298 Method* caller_method, Thread* self,
1299 Method** sp) {
1300 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, false, false);
1301}
1302
Ian Rogers466bb252011-10-14 03:29:56 -07001303static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1304 ScopedLocalRef<jclass> jlr_UTE_class(env,
1305 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1306 if (jlr_UTE_class.get() == NULL) {
1307 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1308 } else {
1309 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1310 "(Ljava/lang/Throwable;)V");
1311 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1312 ScopedLocalRef<jthrowable> jlr_UTE(env,
1313 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1314 jexception)));
1315 int rc = env->Throw(jlr_UTE.get());
1316 if (rc != JNI_OK) {
1317 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1318 }
1319 }
1320 CHECK(self->IsExceptionPending());
1321}
1322
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001323// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1324// which is responsible for recording callee save registers. We explicitly handlerize incoming
1325// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1326// the invocation handler which is a field within the proxy object receiver.
1327extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001328 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001329 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001330 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1331 DCHECK_EQ(*proxy_sp, proxy_method);
1332 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001333 // TODO: ARM specific
1334 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1335 // Start new JNI local reference state
1336 JNIEnvExt* env = self->GetJniEnv();
1337 ScopedJniEnvLocalRefState env_state(env);
1338 // Create local ref. copies of proxy method and the receiver
1339 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1340 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1341
Ian Rogers14b1b242011-10-11 18:54:34 -07001342 // Placing into local references incoming arguments from the caller's register arguments,
1343 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001344 MethodHelper proxy_mh(proxy_method);
1345 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001346 size_t args_in_regs = 0;
1347 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001348 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001349 if (args_in_regs > 2) {
1350 args_in_regs = 2;
1351 break;
1352 }
1353 }
1354 size_t cur_arg = 0; // current stack location to read
1355 size_t param_index = 1; // skip receiver
1356 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001357 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001358 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001359 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001360 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001361 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001362 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001363 param_index++;
1364 }
1365 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001366 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001367 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001368 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001369 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1370 jobject jobj = AddLocalReference<jobject>(env, obj);
1371 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001372 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001373 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001374 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001375 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001376 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1377 jvalue args_jobj[3];
1378 args_jobj[0].l = rcvr_jobj;
1379 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001380 // Args array, if no arguments then NULL (don't include receiver in argument count)
1381 args_jobj[2].l = NULL;
1382 ObjectArray<Object>* args = NULL;
1383 if ((num_params - 1) > 0) {
1384 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001385 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001386 CHECK(self->IsExceptionPending());
1387 return;
1388 }
1389 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1390 }
1391 // Convert proxy method into expected interface method
1392 Method* interface_method = proxy_method->FindOverriddenMethod();
1393 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001394 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001395 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001396 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001397 cur_arg = 0; // reset stack location to read to start
1398 // reset index, will index into param type array which doesn't include the receiver
1399 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001400 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001401 CHECK(param_types != NULL);
1402 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1403 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1404 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1405 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001406 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001407 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001408 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001409 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001410 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1411 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1412 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001413 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001414 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001415 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001416 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001417 if (self->IsExceptionPending()) {
1418 return;
1419 }
1420 obj = val.l;
1421 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001422 args->Set(param_index, obj);
1423 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1424 param_index++;
1425 }
1426 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001427 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1428 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001429 Class* param_type = param_types->Get(param_index);
1430 Object* obj;
1431 if (!param_type->IsPrimitive()) {
1432 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1433 } else {
1434 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001435 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001436 if (self->IsExceptionPending()) {
1437 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001438 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001439 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001440 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001441 args->Set(param_index, obj);
1442 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1443 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001444 }
1445 // Get the InvocationHandler method and the field that holds it within the Proxy object
1446 static jmethodID inv_hand_invoke_mid = NULL;
1447 static jfieldID proxy_inv_hand_fid = NULL;
1448 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001449 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001450 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001451 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001452 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1453 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1454 }
Ian Rogers466bb252011-10-14 03:29:56 -07001455 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001456 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1457 // Call InvocationHandler.invoke
1458 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1459 // Place result in stack args
1460 if (!self->IsExceptionPending()) {
1461 Object* result_ref = self->DecodeJObject(result);
1462 if (result_ref != NULL) {
1463 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001464 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001465 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1466 } else {
1467 *reinterpret_cast<jobject*>(stack_args) = NULL;
1468 }
Ian Rogers466bb252011-10-14 03:29:56 -07001469 } else {
1470 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1471 // a UndeclaredThrowableException.
1472 Throwable* exception = self->GetException();
1473 self->ClearException();
1474 if (!exception->IsCheckedException()) {
1475 self->SetException(exception);
1476 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001477 SynthesizedProxyClass* proxy_class =
1478 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1479 int throws_index = -1;
1480 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1481 for (size_t i = 0; i < num_virt_methods; i++) {
1482 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1483 throws_index = i;
1484 break;
1485 }
1486 }
1487 CHECK_NE(throws_index, -1);
1488 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001489 Class* exception_class = exception->GetClass();
1490 bool declares_exception = false;
1491 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1492 Class* declared_exception = declared_exceptions->Get(i);
1493 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1494 }
1495 if (declares_exception) {
1496 self->SetException(exception);
1497 } else {
1498 ThrowNewUndeclaredThrowableException(self, env, exception);
1499 }
1500 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001501 }
1502}
1503
jeffhaoe343b762011-12-05 16:36:44 -08001504extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001505 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001506 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1507 self->PushTraceStackFrame(trace_frame);
1508
jeffhao2692b572011-12-16 15:42:28 -08001509 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001510
jeffhao2692b572011-12-16 15:42:28 -08001511 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001512}
1513
1514extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001515 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001516 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1517 Method* method = trace_frame.method_;
1518 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001519
jeffhao2692b572011-12-16 15:42:28 -08001520 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001521
1522 return lr;
1523}
1524
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001525uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001526 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001527 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1528 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001529 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001530
jeffhao2692b572011-12-16 15:42:28 -08001531 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001532
1533 return lr;
1534}
1535
Shih-wei Liao2d831012011-09-28 22:06:53 -07001536/*
1537 * Float/double conversion requires clamping to min and max of integer form. If
1538 * target doesn't support this normally, use these.
1539 */
1540int64_t D2L(double d) {
1541 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1542 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1543 if (d >= kMaxLong)
1544 return (int64_t)0x7fffffffffffffffULL;
1545 else if (d <= kMinLong)
1546 return (int64_t)0x8000000000000000ULL;
1547 else if (d != d) // NaN case
1548 return 0;
1549 else
1550 return (int64_t)d;
1551}
1552
1553int64_t F2L(float f) {
1554 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1555 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1556 if (f >= kMaxLong)
1557 return (int64_t)0x7fffffffffffffffULL;
1558 else if (f <= kMinLong)
1559 return (int64_t)0x8000000000000000ULL;
1560 else if (f != f) // NaN case
1561 return 0;
1562 else
1563 return (int64_t)f;
1564}
1565
1566} // namespace art