blob: 7e4e410be51471fb8c2a5d9d4e1eb8ec465a3f4f [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"
Shih-wei Liaob0ee9d72012-03-07 16:39:26 -080026#include "runtime_support_common.h"
jeffhaoe343b762011-12-05 16:36:44 -080027#include "trace.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070028#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070029
Shih-wei Liao2d831012011-09-28 22:06:53 -070030namespace art {
31
Bill Buzbee11f9d212012-03-03 20:03:18 -080032extern "C" int art_cmpl_float(float a, float b) {
33 if (a == b) {
34 return 0;
35 } else if (a < b) {
36 return -1;
37 } else if (a > b) {
38 return 1;
39 }
40 return -1;
41}
42
43extern "C" int art_cmpg_float(float a, float b) {
44 if (a == b) {
45 return 0;
46 } else if (a < b) {
47 return -1;
48 } else if (a > b) {
49 return 1;
50 }
51 return 1;
52}
53
54extern "C" int art_cmpl_double(double a, double b) {
55 if (a == b) {
56 return 0;
57 } else if (a < b) {
58 return -1;
59 } else if (a > b) {
60 return 1;
61 }
62 return -1;
63}
64
65extern "C" int art_cmpg_double(double a, double b) {
66 if (a == b) {
67 return 0;
68 } else if (a < b) {
69 return -1;
70 } else if (a > b) {
71 return 1;
72 }
73 return 1;
74}
75
Ian Rogers4f0d07c2011-10-06 23:38:47 -070076// Place a special frame at the TOS that will save the callee saves for the given type
77static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070078 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070079 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
80 self->SetTopOfStack(sp, 0);
81}
82
buzbee44b412b2012-02-04 08:50:53 -080083/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080084 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -080085 * the method. However, because the offset alone cannot distinguish between
86 * method entry and offset 0 within the method, we'll use an offset of -1
87 * to denote method entry.
88 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080089extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -080090 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080091 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -080092}
93
Shih-wei Liao2d831012011-09-28 22:06:53 -070094// Temporary debugging hook for compiler.
95extern void DebugMe(Method* method, uint32_t info) {
96 LOG(INFO) << "DebugMe";
97 if (method != NULL) {
98 LOG(INFO) << PrettyMethod(method);
99 }
100 LOG(INFO) << "Info: " << info;
101}
102
103// Return value helper for jobject return types
104extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -0700105 if (thread->IsExceptionPending()) {
106 return NULL;
107 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700108 return thread->DecodeJObject(obj);
109}
110
Ian Rogers60db5ab2012-02-20 17:02:00 -0800111extern void* FindNativeMethod(Thread* self) {
112 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700113
Ian Rogers60db5ab2012-02-20 17:02:00 -0800114 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700115 DCHECK(method != NULL);
116
117 // Lookup symbol address for method, on failure we'll return NULL with an
118 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -0800119 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700120 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800121 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700122 return NULL;
123 } else {
124 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -0800125 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700126 return native_code;
127 }
128}
129
130// Called by generated call to throw an exception
131extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
132 /*
133 * exception may be NULL, in which case this routine should
134 * throw NPE. NOTE: this is a convenience for generated code,
135 * which previously did the null check inline and constructed
136 * and threw a NPE if NULL. This routine responsible for setting
137 * exception_ in thread and delivering the exception.
138 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700139 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700140 if (exception == NULL) {
141 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
142 } else {
143 thread->SetException(exception);
144 }
145 thread->DeliverException();
146}
147
148// Deliver an exception that's pending on thread helping set up a callee save frame on the way
149extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700150 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700151 thread->DeliverException();
152}
153
154// Called by generated call to throw a NPE exception
155extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700156 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700157 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700158 thread->DeliverException();
159}
160
161// Called by generated call to throw an arithmetic divide by zero exception
162extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700163 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700164 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
165 thread->DeliverException();
166}
167
168// Called by generated call to throw an arithmetic divide by zero exception
169extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700170 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
171 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
172 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700173 thread->DeliverException();
174}
175
176// Called by the AbstractMethodError stub (not runtime support)
177extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700178 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
179 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
180 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700181 thread->DeliverException();
182}
183
184extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700185 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800186 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800187 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800188 artTraceMethodUnwindFromCode(thread);
189 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700190 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700191 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
192 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700193 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700194 thread->ResetDefaultStackEnd(); // Return to default stack size
195 thread->DeliverException();
196}
197
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700198static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700199 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700200 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
201 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
202
203 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700204 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700205 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
206 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700207 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700208 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
209 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700210 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700211 type_idx = ref;
212 } else {
213 CHECK(false) << static_cast<int>(ref_type);
214 }
215
Ian Rogers0571d352011-11-03 19:51:38 -0700216 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700217 if (!access) {
218 return class_name;
219 }
220
221 std::string result;
222 result += "tried to access class ";
223 result += class_name;
224 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700226 return result;
227}
228
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700229static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700230 verifier::VerifyErrorRefType ref_type, bool access) {
231 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700232
233 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
234 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
235
236 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700237 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700238 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700239 if (!access) {
240 return class_name + "." + field_name;
241 }
242
243 std::string result;
244 result += "tried to access field ";
245 result += class_name + "." + field_name;
246 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700248 return result;
249}
250
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700251static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800252 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700253 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700254
255 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
256 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
257
258 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700259 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700260 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700261 if (!access) {
262 return class_name + "." + method_name;
263 }
264
265 std::string result;
266 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700267 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700268 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700269 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700271 return result;
272}
273
274extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700275 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
276 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700277 frame.Next();
278 Method* method = frame.GetMethod();
279
Ian Rogersd81871c2011-10-03 13:57:23 -0700280 verifier::VerifyErrorRefType ref_type =
281 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700282
283 const char* exception_class = "Ljava/lang/VerifyError;";
284 std::string msg;
285
Ian Rogersd81871c2011-10-03 13:57:23 -0700286 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
287 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700288 exception_class = "Ljava/lang/NoClassDefFoundError;";
289 msg = ClassNameFromIndex(method, ref, ref_type, false);
290 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700291 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700292 exception_class = "Ljava/lang/NoSuchFieldError;";
293 msg = FieldNameFromIndex(method, ref, ref_type, false);
294 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700295 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700296 exception_class = "Ljava/lang/NoSuchMethodError;";
297 msg = MethodNameFromIndex(method, ref, ref_type, false);
298 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700299 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700300 exception_class = "Ljava/lang/IllegalAccessError;";
301 msg = ClassNameFromIndex(method, ref, ref_type, true);
302 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700303 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700304 exception_class = "Ljava/lang/IllegalAccessError;";
305 msg = FieldNameFromIndex(method, ref, ref_type, true);
306 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700307 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700308 exception_class = "Ljava/lang/IllegalAccessError;";
309 msg = MethodNameFromIndex(method, ref, ref_type, true);
310 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700311 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700312 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
313 msg = ClassNameFromIndex(method, ref, ref_type, false);
314 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700315 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700316 exception_class = "Ljava/lang/InstantiationError;";
317 msg = ClassNameFromIndex(method, ref, ref_type, false);
318 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700320 // Generic VerifyError; use default exception, no message.
321 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700322 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700323 CHECK(false);
324 break;
325 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700326 self->ThrowNewException(exception_class, msg.c_str());
327 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700328}
329
330extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700331 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700332 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700333 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700334 thread->DeliverException();
335}
336
337extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700338 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700339 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700340 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700341 thread->DeliverException();
342}
343
Elliott Hughese1410a22011-10-04 12:10:24 -0700344extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700345 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
346 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700347 frame.Next();
348 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700349 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700350 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700351 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700352}
353
354extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700355 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700356 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700357 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700358 thread->DeliverException();
359}
360
Ian Rogers19846512012-02-24 11:42:47 -0800361const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread,
362 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700363 // TODO: this code is specific to ARM
364 // On entry the stack pointed by sp is:
365 // | argN | |
366 // | ... | |
367 // | arg4 | |
368 // | arg3 spill | | Caller's frame
369 // | arg2 spill | |
370 // | arg1 spill | |
371 // | Method* | ---
372 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700373 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700374 // | R3 | arg3
375 // | R2 | arg2
376 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700377 // | R0 |
378 // | Method* | <- sp
379 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
380 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
381 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
382 uintptr_t caller_pc = regs[10];
383 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700384 // Start new JNI local reference state
385 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700386 ScopedJniEnvLocalRefState env_state(env);
Ian Rogers19846512012-02-24 11:42:47 -0800387
388 // Compute details about the called method (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700389 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers19846512012-02-24 11:42:47 -0800390 Method* caller = *caller_sp;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700391 bool is_static;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800392 bool is_virtual;
Ian Rogers19846512012-02-24 11:42:47 -0800393 uint32_t dex_method_idx;
394 const char* shorty;
395 uint32_t shorty_len;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700396 if (type == Runtime::kUnknownMethod) {
Ian Rogers19846512012-02-24 11:42:47 -0800397 DCHECK(called->IsRuntimeMethod());
Ian Rogersdf9a7822011-10-11 16:53:22 -0700398 // less two as return address may span into next dex instruction
399 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800400 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700401 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
402 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700403 Instruction::Code instr_code = instr->Opcode();
404 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
405 (instr_code == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800406 is_virtual = (instr_code == Instruction::INVOKE_VIRTUAL) ||
407 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700408 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
Ian Rogersfb6adba2012-03-04 21:51:51 -0800409 (instr_code == Instruction::INVOKE_DIRECT_RANGE) ||
410 (instr_code == Instruction::INVOKE_VIRTUAL) ||
411 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE));
Elliott Hughesadb8c672012-03-06 16:49:32 -0800412 DecodedInstruction dec_insn(instr);
413 dex_method_idx = dec_insn.vB;
Ian Rogers19846512012-02-24 11:42:47 -0800414 shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700415 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800416 DCHECK(!called->IsRuntimeMethod());
Ian Rogersea2a11d2011-10-11 16:48:51 -0700417 is_static = type == Runtime::kStaticMethod;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800418 is_virtual = false;
Ian Rogers19846512012-02-24 11:42:47 -0800419 dex_method_idx = called->GetDexMethodIndex();
420 MethodHelper mh(called);
421 shorty = mh.GetShorty();
422 shorty_len = mh.GetShortyLength();
423 }
424 // Discover shorty (avoid GCs)
425 size_t args_in_regs = 0;
426 for (size_t i = 1; i < shorty_len; i++) {
427 char c = shorty[i];
428 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
429 if (args_in_regs > 3) {
430 args_in_regs = 3;
431 break;
432 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700433 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700434 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700435 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700436 if (!is_static) {
437 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
438 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700439 if (args_in_regs < 3) {
440 // If we thought we had fewer than 3 arguments in registers, account for the receiver
441 args_in_regs++;
442 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700443 AddLocalReference<jobject>(env, obj);
444 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700445 size_t shorty_index = 1; // skip return value
446 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
447 // R0)
448 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
449 char c = shorty[shorty_index];
450 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700451 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700452 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
453 AddLocalReference<jobject>(env, obj);
454 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700455 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
456 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700457 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700458 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700459 while (shorty_index < shorty_len) {
460 char c = shorty[shorty_index];
461 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700462 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700463 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700464 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700465 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700466 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700467 }
468 // Resolve method filling in dex cache
Ian Rogers19846512012-02-24 11:42:47 -0800469 if (type == Runtime::kUnknownMethod) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800470 called = linker->ResolveMethod(dex_method_idx, caller, !is_virtual);
Ian Rogers19846512012-02-24 11:42:47 -0800471 }
472 const void* code = NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700473 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800474 if (LIKELY(called->IsDirect() == !is_virtual)) {
Ian Rogers19846512012-02-24 11:42:47 -0800475 // Ensure that the called method's class is initialized.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800476 Class* called_class = called->GetDeclaringClass();
477 linker->EnsureInitialized(called_class, true);
478 if (LIKELY(called_class->IsInitialized())) {
Ian Rogers19846512012-02-24 11:42:47 -0800479 code = called->GetCode();
480 } else if (called_class->IsInitializing()) {
481 // Class is still initializing, go to oat and grab code (trampoline must be left in place
482 // until class is initialized to stop races between threads).
483 code = linker->GetOatCodeFor(called);
484 } else {
485 DCHECK(called_class->IsErroneous());
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800486 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800487 } else {
488 // Direct method has been made virtual
489 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
490 "Expected direct method but found virtual: %s",
491 PrettyMethod(called, true).c_str());
492 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700493 }
Ian Rogers19846512012-02-24 11:42:47 -0800494 if (UNLIKELY(code == NULL)) {
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 {
Ian Rogers19846512012-02-24 11:42:47 -0800501 // Expect class to at least be initializing.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800502 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogers19846512012-02-24 11:42:47 -0800503 // Don't want infinite recursion.
504 DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData());
Ian Rogersad25ac52011-10-04 19:13:33 -0700505 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700506 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700507 }
508 return code;
509}
510
Ian Rogers60db5ab2012-02-20 17:02:00 -0800511static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
512 intptr_t value = *arg_ptr;
513 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
514 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
515 CHECK(Heap::IsHeapAddress(value_as_work_around_rep));
516 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
517}
518
519extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
520 DCHECK(Thread::Current() == self);
521 // TODO: this code is specific to ARM
522 // On entry the stack pointed by sp is:
523 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
524 // | LR |
525 // | R3 | arg2
526 // | R2 | arg1
527 // | R1 | jclass/jobject
528 // | R0 | JNIEnv
529 // | unused |
530 // | unused |
531 // | unused | <- sp
532 Method* jni_method = self->GetTopOfStack().GetMethod();
533 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
534 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
535 // Fix up this/jclass argument
536 WorkAroundJniBugsForJobject(arg_ptr);
537 arg_ptr++;
538 // Fix up jobject arguments
539 MethodHelper mh(jni_method);
540 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800541 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800542 char shorty_char = mh.GetShorty()[i];
543 if (shorty_char == 'L') {
544 WorkAroundJniBugsForJobject(arg_ptr);
545 }
546 if (shorty_char == 'J' || shorty_char == 'D') {
547 if (reg_num == 2) {
548 arg_ptr = sp + 8; // skip to out arguments
549 reg_num = -1;
550 } else if (reg_num == 3) {
551 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
552 reg_num = -1;
553 } else {
554 DCHECK(reg_num == -1);
555 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
556 arg_ptr += 3; // unaligned, pad and move through stack arguments
557 } else {
558 arg_ptr += 2; // aligned, move through stack arguments
559 }
560 }
561 } else {
562 if (reg_num == 2) {
563 arg_ptr++; // move through register arguments
564 reg_num++;
565 } else if (reg_num == 3) {
566 arg_ptr = sp + 8; // skip to outgoing stack arguments
567 reg_num = -1;
568 } else {
569 DCHECK(reg_num == -1);
570 arg_ptr++; // move through stack arguments
571 }
572 }
573 }
574 // Load expected destination, see Method::RegisterNative
Ian Rogers19846512012-02-24 11:42:47 -0800575 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
576 if (UNLIKELY(code == NULL)) {
577 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
578 jni_method->RegisterNative(self, code);
579 }
580 return code;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800581}
582
583
Ian Rogersce9eca62011-10-07 17:11:03 -0700584extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
585 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800586 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700587 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800588 return field->Get32(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700589 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700590 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800591 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800592 if (LIKELY(field != NULL)) {
593 return field->Get32(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700594 }
595 return 0; // Will throw exception by checking with Thread::Current
596}
597
598extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
599 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800600 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700601 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800602 return field->Get64(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700603 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700604 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800605 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800606 if (LIKELY(field != NULL)) {
607 return field->Get64(NULL);
Ian Rogersce9eca62011-10-07 17:11:03 -0700608 }
609 return 0; // Will throw exception by checking with Thread::Current
610}
611
612extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
613 Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800614 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700615 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800616 return field->GetObj(NULL);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700617 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700618 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800619 field = FindFieldFromCode(field_idx, referrer, self, true, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800620 if (LIKELY(field != NULL)) {
621 return field->GetObj(NULL);
622 }
623 return NULL; // Will throw exception by checking with Thread::Current
624}
625
626extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
627 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800628 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800629 if (LIKELY(field != NULL && obj != NULL)) {
630 return field->Get32(obj);
631 }
632 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800633 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800634 if (LIKELY(field != NULL)) {
635 if (UNLIKELY(obj == NULL)) {
636 ThrowNullPointerExceptionForFieldAccess(self, field, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700637 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800638 return field->Get32(obj);
639 }
640 }
641 return 0; // Will throw exception by checking with Thread::Current
642}
643
644extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
645 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800646 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800647 if (LIKELY(field != NULL && obj != NULL)) {
648 return field->Get64(obj);
649 }
650 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800651 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800652 if (LIKELY(field != NULL)) {
653 if (UNLIKELY(obj == NULL)) {
654 ThrowNullPointerExceptionForFieldAccess(self, field, true);
655 } else {
656 return field->Get64(obj);
657 }
658 }
659 return 0; // Will throw exception by checking with Thread::Current
660}
661
662extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
663 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800664 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800665 if (LIKELY(field != NULL && obj != NULL)) {
666 return field->GetObj(obj);
667 }
668 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800669 field = FindFieldFromCode(field_idx, referrer, self, false, false, false, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800670 if (LIKELY(field != NULL)) {
671 if (UNLIKELY(obj == NULL)) {
672 ThrowNullPointerExceptionForFieldAccess(self, field, true);
673 } else {
674 return field->GetObj(obj);
Ian Rogersce9eca62011-10-07 17:11:03 -0700675 }
676 }
677 return NULL; // Will throw exception by checking with Thread::Current
678}
679
Ian Rogers1bddec32012-02-04 12:27:34 -0800680extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
681 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800682 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700683 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800684 field->Set32(NULL, new_value);
685 return 0; // success
Ian Rogerscaab8c42011-10-12 12:11:18 -0700686 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700687 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800688 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800689 if (LIKELY(field != NULL)) {
690 field->Set32(NULL, new_value);
691 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700692 }
693 return -1; // failure
694}
695
696extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
697 uint64_t new_value, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800698 Field* field = FindFieldFast(field_idx, referrer, 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 Rogerscaab8c42011-10-12 12:11:18 -0700702 }
703 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800704 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int64_t));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700705 if (LIKELY(field != NULL)) {
Ian Rogers1bddec32012-02-04 12:27:34 -0800706 field->Set64(NULL, new_value);
707 return 0; // success
Ian Rogersce9eca62011-10-07 17:11:03 -0700708 }
709 return -1; // failure
710}
711
Ian Rogers1bddec32012-02-04 12:27:34 -0800712extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
713 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800714 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
Ian Rogerscaab8c42011-10-12 12:11:18 -0700715 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800716 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700717 field->SetObj(NULL, new_value);
718 return 0; // success
719 }
720 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700721 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800722 field = FindFieldFromCode(field_idx, referrer, self, true, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800723 if (LIKELY(field != NULL)) {
724 field->SetObj(NULL, new_value);
725 return 0; // success
726 }
727 return -1; // failure
728}
729
730extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
731 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800732 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800733 if (LIKELY(field != NULL && obj != NULL)) {
734 field->Set32(obj, new_value);
735 return 0; // success
736 }
737 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800738 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int32_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800739 if (LIKELY(field != NULL)) {
740 if (UNLIKELY(obj == NULL)) {
741 ThrowNullPointerExceptionForFieldAccess(self, field, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700742 } else {
Ian Rogers1bddec32012-02-04 12:27:34 -0800743 field->Set32(obj, new_value);
744 return 0; // success
745 }
746 }
747 return -1; // failure
748}
749
750extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
751 Thread* self, Method** sp) {
752 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
753 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
jeffhao8cd6dda2012-02-22 10:15:34 -0800754 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800755 if (LIKELY(field != NULL && obj != NULL)) {
756 field->Set64(obj, new_value);
757 return 0; // success
758 }
759 *sp = callee_save;
760 self->SetTopOfStack(sp, 0);
jeffhao8cd6dda2012-02-22 10:15:34 -0800761 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int64_t));
Ian Rogers1bddec32012-02-04 12:27:34 -0800762 if (LIKELY(field != NULL)) {
763 if (UNLIKELY(obj == NULL)) {
764 ThrowNullPointerExceptionForFieldAccess(self, field, false);
765 } else {
766 field->Set64(obj, new_value);
767 return 0; // success
768 }
769 }
770 return -1; // failure
771}
772
773extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
774 const Method* referrer, Thread* self, Method** sp) {
jeffhao8cd6dda2012-02-22 10:15:34 -0800775 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800776 if (LIKELY(field != NULL && obj != NULL)) {
777 field->SetObj(obj, new_value);
778 return 0; // success
779 }
780 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
jeffhao8cd6dda2012-02-22 10:15:34 -0800781 field = FindFieldFromCode(field_idx, referrer, self, false, false, true, sizeof(Object*));
Ian Rogers1bddec32012-02-04 12:27:34 -0800782 if (LIKELY(field != NULL)) {
783 if (UNLIKELY(obj == NULL)) {
784 ThrowNullPointerExceptionForFieldAccess(self, field, false);
785 } else {
786 field->SetObj(obj, new_value);
Ian Rogersce9eca62011-10-07 17:11:03 -0700787 return 0; // success
788 }
789 }
790 return -1; // failure
791}
792
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800793extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
794 Thread* self, Method** sp) {
795 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
796 return AllocObjectFromCode(type_idx, method, self, false);
797}
798
Ian Rogers28ad40d2011-10-27 15:19:26 -0700799extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
800 Thread* self, Method** sp) {
801 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800802 return AllocObjectFromCode(type_idx, method, self, true);
803}
804
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800805extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
806 Thread* self, Method** sp) {
807 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
808 return AllocArrayFromCode(type_idx, method, component_count, self, false);
809}
810
811extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
812 int32_t component_count,
813 Thread* self, Method** sp) {
814 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
815 return AllocArrayFromCode(type_idx, method, component_count, self, true);
816}
817
Ian Rogersce9eca62011-10-07 17:11:03 -0700818extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
819 int32_t component_count, Thread* self, Method** sp) {
820 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800821 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700822}
823
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800824extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
825 int32_t component_count,
826 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700827 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800828 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700829}
830
Ian Rogerscaab8c42011-10-12 12:11:18 -0700831// Assignable test for code, won't throw. Null and equality tests already performed
832uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
833 DCHECK(klass != NULL);
834 DCHECK(ref_class != NULL);
835 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
836}
837
Shih-wei Liao2d831012011-09-28 22:06:53 -0700838// 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 -0700839extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700840 DCHECK(a->IsClass()) << PrettyClass(a);
841 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700842 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700843 return 0; // Success
844 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700845 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700846 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700847 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800848 PrettyDescriptor(a).c_str(),
849 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700850 return -1; // Failure
851 }
852}
853
854// Tests whether 'element' can be assigned into an array of type 'array_class'.
855// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700856extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
857 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700858 DCHECK(array_class != NULL);
859 // element can't be NULL as we catch this is screened in runtime_support
860 Class* element_class = element->GetClass();
861 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700862 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700863 return 0; // Success
864 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700865 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700866 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800867 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800868 PrettyDescriptor(element_class).c_str(),
869 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700870 return -1; // Failure
871 }
872}
873
Elliott Hughesf3778f62012-01-26 14:14:35 -0800874Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
875 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700876 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
877 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700878 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700879 CHECK(self->IsExceptionPending());
880 return NULL; // Failure - Indicate to caller to deliver exception
881 }
Elliott Hughesf3778f62012-01-26 14:14:35 -0800882 // Perform access check if necessary.
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800883 Class* referring_class = referrer->GetDeclaringClass();
884 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
885 ThrowNewIllegalAccessErrorClass(self, referring_class, klass);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800886 return NULL; // Failure - Indicate to caller to deliver exception
887 }
888 // If we're just implementing const-class, we shouldn't call <clinit>.
889 if (!can_run_clinit) {
890 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -0700891 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700892 // If we are the <clinit> of this class, just return our storage.
893 //
894 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
895 // running.
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800896 if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700897 return klass;
898 }
899 if (!class_linker->EnsureInitialized(klass, true)) {
900 CHECK(self->IsExceptionPending());
901 return NULL; // Failure - Indicate to caller to deliver exception
902 }
903 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
904 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700905}
906
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700907extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
908 Thread* self, Method** sp) {
909 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800910 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700911}
912
Ian Rogers28ad40d2011-10-27 15:19:26 -0700913extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
914 Method** sp) {
915 // Called when method->dex_cache_resolved_types_[] misses
916 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800917 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700918}
919
Ian Rogersb093c6b2011-10-31 16:19:55 -0700920extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
921 const Method* referrer, Thread* self,
922 Method** sp) {
923 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
924 // unpopulated
925 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800926 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700927}
928
Brian Carlstromaded5f72011-10-07 17:15:04 -0700929String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
930 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
931 return class_linker->ResolveString(string_idx, referrer);
932}
933
934extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
935 Thread* self, Method** sp) {
936 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
937 return ResolveStringFromCode(referrer, string_idx);
938}
939
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700940extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
941 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700942 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700943 // MonitorExit may throw exception
944 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
945}
946
947extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
948 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
949 DCHECK(obj != NULL); // Assumed to have been checked before entry
950 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700951 DCHECK(thread->HoldsLock(obj));
952 // Only possible exception is NPE and is handled before entry
953 DCHECK(!thread->IsExceptionPending());
954}
955
Ian Rogers4a510d82011-10-09 14:30:24 -0700956void CheckSuspendFromCode(Thread* thread) {
957 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700958 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
959}
960
Ian Rogers4a510d82011-10-09 14:30:24 -0700961extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
962 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700963 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700964 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
965}
966
967/*
968 * Fill the array with predefined constant values, throwing exceptions if the array is null or
969 * not of sufficient length.
970 *
971 * NOTE: When dealing with a raw dex file, the data to be copied uses
972 * little-endian ordering. Require that oat2dex do any required swapping
973 * so this routine can get by with a memcpy().
974 *
975 * Format of the data:
976 * ushort ident = 0x0300 magic value
977 * ushort width width of each element in the table
978 * uint size number of elements in the table
979 * ubyte data[size*width] table of data values (may contain a single-byte
980 * padding at the end)
981 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700982extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
983 Thread* self, Method** sp) {
984 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700985 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700986 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700987 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
988 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700989 return -1; // Error
990 }
991 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
992 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700993 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700994 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
995 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700996 return -1; // Error
997 }
998 uint16_t width = table[1];
999 uint32_t size_in_bytes = size * width;
Ian Rogersa15e67d2012-02-28 13:51:55 -08001000 memcpy((char*)array + Array::DataOffset(width).Int32Value(), (char*)&table[4], size_in_bytes);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001001 return 0; // Success
1002}
1003
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001004static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
Ian Rogersc8b306f2012-02-17 21:34:44 -08001005 Thread* self, Method** sp, bool access_check, InvokeType type){
1006 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001007 if (UNLIKELY(method == NULL)) {
1008 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001009 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
1010 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001011 return 0; // failure
1012 }
Ian Rogersc8b306f2012-02-17 21:34:44 -08001013 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001014 if (UNLIKELY(method == NULL)) {
1015 CHECK(self->IsExceptionPending());
1016 return 0; // failure
Ian Rogerscaab8c42011-10-12 12:11:18 -07001017 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001018 }
Ian Rogers19846512012-02-24 11:42:47 -08001019 DCHECK(!self->IsExceptionPending());
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001020 const void* code = method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001021
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001022 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001023 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1024 uint64_t result = ((code_uint << 32) | method_uint);
1025 return result;
1026}
1027
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001028// See comments in runtime_support_asm.S
1029extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1030 Method* caller_method, Thread* self,
1031 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001032 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001033}
1034
1035extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1036 Object* this_object,
1037 Method* caller_method, Thread* self,
1038 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001039 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1040}
1041
1042
1043extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1044 Object* this_object,
1045 Method* caller_method, Thread* self,
1046 Method** sp) {
1047 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1048}
1049
1050extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1051 Object* this_object,
1052 Method* caller_method, Thread* self,
1053 Method** sp) {
1054 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001055}
1056
1057extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1058 Object* this_object,
1059 Method* caller_method, Thread* self,
1060 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001061 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001062}
1063
1064extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1065 Object* this_object,
1066 Method* caller_method, Thread* self,
1067 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001068 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001069}
1070
Ian Rogers466bb252011-10-14 03:29:56 -07001071static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1072 ScopedLocalRef<jclass> jlr_UTE_class(env,
1073 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1074 if (jlr_UTE_class.get() == NULL) {
1075 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1076 } else {
1077 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1078 "(Ljava/lang/Throwable;)V");
1079 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1080 ScopedLocalRef<jthrowable> jlr_UTE(env,
1081 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1082 jexception)));
1083 int rc = env->Throw(jlr_UTE.get());
1084 if (rc != JNI_OK) {
1085 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1086 }
1087 }
1088 CHECK(self->IsExceptionPending());
1089}
1090
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001091// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1092// which is responsible for recording callee save registers. We explicitly handlerize incoming
1093// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1094// the invocation handler which is a field within the proxy object receiver.
1095extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001096 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001097 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001098 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1099 DCHECK_EQ(*proxy_sp, proxy_method);
1100 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001101 // TODO: ARM specific
1102 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1103 // Start new JNI local reference state
1104 JNIEnvExt* env = self->GetJniEnv();
1105 ScopedJniEnvLocalRefState env_state(env);
1106 // Create local ref. copies of proxy method and the receiver
1107 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1108 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1109
Ian Rogers14b1b242011-10-11 18:54:34 -07001110 // Placing into local references incoming arguments from the caller's register arguments,
1111 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001112 MethodHelper proxy_mh(proxy_method);
1113 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001114 size_t args_in_regs = 0;
1115 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001116 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001117 if (args_in_regs > 2) {
1118 args_in_regs = 2;
1119 break;
1120 }
1121 }
1122 size_t cur_arg = 0; // current stack location to read
1123 size_t param_index = 1; // skip receiver
1124 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001125 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001126 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001127 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001128 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001129 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001130 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001131 param_index++;
1132 }
1133 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001134 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001135 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001136 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001137 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1138 jobject jobj = AddLocalReference<jobject>(env, obj);
1139 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001140 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001141 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001142 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001143 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001144 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1145 jvalue args_jobj[3];
1146 args_jobj[0].l = rcvr_jobj;
1147 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001148 // Args array, if no arguments then NULL (don't include receiver in argument count)
1149 args_jobj[2].l = NULL;
1150 ObjectArray<Object>* args = NULL;
1151 if ((num_params - 1) > 0) {
1152 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001153 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001154 CHECK(self->IsExceptionPending());
1155 return;
1156 }
1157 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1158 }
1159 // Convert proxy method into expected interface method
1160 Method* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers19846512012-02-24 11:42:47 -08001161 DCHECK(interface_method != NULL);
1162 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001163 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001164 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001165 cur_arg = 0; // reset stack location to read to start
1166 // reset index, will index into param type array which doesn't include the receiver
1167 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001168 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers19846512012-02-24 11:42:47 -08001169 DCHECK(param_types != NULL);
Ian Rogers14b1b242011-10-11 18:54:34 -07001170 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
Ian Rogers19846512012-02-24 11:42:47 -08001171 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001172 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1173 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001174 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001175 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001176 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001177 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001178 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1179 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1180 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001181 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001182 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001183 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001184 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001185 if (self->IsExceptionPending()) {
1186 return;
1187 }
1188 obj = val.l;
1189 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001190 args->Set(param_index, obj);
1191 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1192 param_index++;
1193 }
1194 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001195 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1196 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001197 Class* param_type = param_types->Get(param_index);
1198 Object* obj;
1199 if (!param_type->IsPrimitive()) {
1200 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1201 } else {
1202 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001203 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001204 if (self->IsExceptionPending()) {
1205 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001206 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001207 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001208 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001209 args->Set(param_index, obj);
1210 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1211 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001212 }
1213 // Get the InvocationHandler method and the field that holds it within the Proxy object
1214 static jmethodID inv_hand_invoke_mid = NULL;
1215 static jfieldID proxy_inv_hand_fid = NULL;
1216 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001217 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001218 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001219 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001220 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1221 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1222 }
Ian Rogers466bb252011-10-14 03:29:56 -07001223 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001224 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1225 // Call InvocationHandler.invoke
1226 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1227 // Place result in stack args
1228 if (!self->IsExceptionPending()) {
1229 Object* result_ref = self->DecodeJObject(result);
1230 if (result_ref != NULL) {
1231 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001232 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001233 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1234 } else {
1235 *reinterpret_cast<jobject*>(stack_args) = NULL;
1236 }
Ian Rogers466bb252011-10-14 03:29:56 -07001237 } else {
1238 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1239 // a UndeclaredThrowableException.
1240 Throwable* exception = self->GetException();
1241 self->ClearException();
1242 if (!exception->IsCheckedException()) {
1243 self->SetException(exception);
1244 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001245 SynthesizedProxyClass* proxy_class =
1246 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1247 int throws_index = -1;
1248 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1249 for (size_t i = 0; i < num_virt_methods; i++) {
1250 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1251 throws_index = i;
1252 break;
1253 }
1254 }
1255 CHECK_NE(throws_index, -1);
1256 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001257 Class* exception_class = exception->GetClass();
1258 bool declares_exception = false;
1259 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1260 Class* declared_exception = declared_exceptions->Get(i);
1261 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1262 }
1263 if (declares_exception) {
1264 self->SetException(exception);
1265 } else {
1266 ThrowNewUndeclaredThrowableException(self, env, exception);
1267 }
1268 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001269 }
1270}
1271
jeffhaoe343b762011-12-05 16:36:44 -08001272extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001273 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001274 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1275 self->PushTraceStackFrame(trace_frame);
1276
jeffhao2692b572011-12-16 15:42:28 -08001277 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001278
jeffhao2692b572011-12-16 15:42:28 -08001279 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001280}
1281
1282extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001283 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001284 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1285 Method* method = trace_frame.method_;
1286 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001287
jeffhao2692b572011-12-16 15:42:28 -08001288 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001289
1290 return lr;
1291}
1292
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001293uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001294 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001295 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1296 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001297 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001298
jeffhao2692b572011-12-16 15:42:28 -08001299 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001300
1301 return lr;
1302}
1303
Shih-wei Liao2d831012011-09-28 22:06:53 -07001304/*
1305 * Float/double conversion requires clamping to min and max of integer form. If
1306 * target doesn't support this normally, use these.
1307 */
1308int64_t D2L(double d) {
1309 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1310 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1311 if (d >= kMaxLong)
1312 return (int64_t)0x7fffffffffffffffULL;
1313 else if (d <= kMinLong)
1314 return (int64_t)0x8000000000000000ULL;
1315 else if (d != d) // NaN case
1316 return 0;
1317 else
1318 return (int64_t)d;
1319}
1320
1321int64_t F2L(float f) {
1322 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1323 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1324 if (f >= kMaxLong)
1325 return (int64_t)0x7fffffffffffffffULL;
1326 else if (f <= kMinLong)
1327 return (int64_t)0x8000000000000000ULL;
1328 else if (f != f) // NaN case
1329 return 0;
1330 else
1331 return (int64_t)f;
1332}
1333
1334} // namespace art