blob: a1c1be4445bfb918d176b3d1ad13bf4f6ddb1822 [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
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -080032// Place a special frame at the TOS that will save the callee saves for the given type
33static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
34 // Be aware the store below may well stomp on an incoming argument
35 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
36 self->SetTopOfStack(sp, 0);
jeffhao25045522012-03-13 19:34:37 -070037 self->VerifyStack();
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -080038}
39
buzbee44b412b2012-02-04 08:50:53 -080040/*
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080041 * Report location to debugger. Note: dex_pc is the current offset within
buzbee44b412b2012-02-04 08:50:53 -080042 * the method. However, because the offset alone cannot distinguish between
43 * method entry and offset 0 within the method, we'll use an offset of -1
44 * to denote method entry.
45 */
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080046extern "C" void artUpdateDebuggerFromCode(int32_t dex_pc, Thread* self, Method** sp) {
buzbee44b412b2012-02-04 08:50:53 -080047 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080048 Dbg::UpdateDebugger(dex_pc, self, sp);
buzbee44b412b2012-02-04 08:50:53 -080049}
50
Shih-wei Liao2d831012011-09-28 22:06:53 -070051// Temporary debugging hook for compiler.
52extern void DebugMe(Method* method, uint32_t info) {
53 LOG(INFO) << "DebugMe";
54 if (method != NULL) {
55 LOG(INFO) << PrettyMethod(method);
56 }
57 LOG(INFO) << "Info: " << info;
58}
59
60// Return value helper for jobject return types
61extern Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
Brian Carlstrom6f495f22011-10-10 15:05:03 -070062 if (thread->IsExceptionPending()) {
63 return NULL;
64 }
Shih-wei Liao2d831012011-09-28 22:06:53 -070065 return thread->DecodeJObject(obj);
66}
67
Ian Rogers60db5ab2012-02-20 17:02:00 -080068extern void* FindNativeMethod(Thread* self) {
69 DCHECK(Thread::Current() == self);
Shih-wei Liao2d831012011-09-28 22:06:53 -070070
Ian Rogers60db5ab2012-02-20 17:02:00 -080071 Method* method = const_cast<Method*>(self->GetCurrentMethod());
Shih-wei Liao2d831012011-09-28 22:06:53 -070072 DCHECK(method != NULL);
73
74 // Lookup symbol address for method, on failure we'll return NULL with an
75 // exception set, otherwise we return the address of the method we found.
Ian Rogers60db5ab2012-02-20 17:02:00 -080076 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
Shih-wei Liao2d831012011-09-28 22:06:53 -070077 if (native_code == NULL) {
Ian Rogers60db5ab2012-02-20 17:02:00 -080078 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -070079 return NULL;
80 } else {
81 // Register so that future calls don't come here
Ian Rogers60db5ab2012-02-20 17:02:00 -080082 method->RegisterNative(self, native_code);
Shih-wei Liao2d831012011-09-28 22:06:53 -070083 return native_code;
84 }
85}
86
87// Called by generated call to throw an exception
88extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) {
89 /*
90 * exception may be NULL, in which case this routine should
91 * throw NPE. NOTE: this is a convenience for generated code,
92 * which previously did the null check inline and constructed
93 * and threw a NPE if NULL. This routine responsible for setting
94 * exception_ in thread and delivering the exception.
95 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -070096 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -070097 if (exception == NULL) {
98 thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception");
99 } else {
100 thread->SetException(exception);
101 }
102 thread->DeliverException();
103}
104
105// Deliver an exception that's pending on thread helping set up a callee save frame on the way
106extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700107 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700108 thread->DeliverException();
109}
110
111// Called by generated call to throw a NPE exception
Ian Rogers98d39882012-03-15 01:42:12 -0700112extern "C" void artThrowNullPointerExceptionFromCode(Thread* self, Method** sp) {
113 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
114 Frame fr = self->GetTopOfStack();
115 uintptr_t throw_native_pc = fr.GetReturnPC();
116 fr.Next();
117 Method* throw_method = fr.GetMethod();
118 uint32_t dex_pc = throw_method->ToDexPC(throw_native_pc - 2);
119 const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem();
120 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
121 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
122 DecodedInstruction dec_insn(instr);
123 switch (instr->Opcode()) {
124 case Instruction::INVOKE_DIRECT:
125 case Instruction::INVOKE_DIRECT_RANGE:
126 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kDirect);
127 break;
128 case Instruction::INVOKE_VIRTUAL:
129 case Instruction::INVOKE_VIRTUAL_RANGE:
130 ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kVirtual);
131 break;
132 case Instruction::IGET:
133 case Instruction::IGET_WIDE:
134 case Instruction::IGET_OBJECT:
135 case Instruction::IGET_BOOLEAN:
136 case Instruction::IGET_BYTE:
137 case Instruction::IGET_CHAR:
138 case Instruction::IGET_SHORT: {
139 Field* field =
140 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
141 ThrowNullPointerExceptionForFieldAccess(self, field, true /* read */);
142 break;
143 }
144 case Instruction::IPUT:
145 case Instruction::IPUT_WIDE:
146 case Instruction::IPUT_OBJECT:
147 case Instruction::IPUT_BOOLEAN:
148 case Instruction::IPUT_BYTE:
149 case Instruction::IPUT_CHAR:
150 case Instruction::IPUT_SHORT: {
151 Field* field =
152 Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false);
153 ThrowNullPointerExceptionForFieldAccess(self, field, false /* write */);
154 break;
155 }
156 case Instruction::AGET:
157 case Instruction::AGET_WIDE:
158 case Instruction::AGET_OBJECT:
159 case Instruction::AGET_BOOLEAN:
160 case Instruction::AGET_BYTE:
161 case Instruction::AGET_CHAR:
162 case Instruction::AGET_SHORT:
163 self->ThrowNewException("Ljava/lang/NullPointerException;",
164 "Attempt to read from null array");
165 break;
166 case Instruction::APUT:
167 case Instruction::APUT_WIDE:
168 case Instruction::APUT_OBJECT:
169 case Instruction::APUT_BOOLEAN:
170 case Instruction::APUT_BYTE:
171 case Instruction::APUT_CHAR:
172 case Instruction::APUT_SHORT:
173 self->ThrowNewException("Ljava/lang/NullPointerException;",
174 "Attempt to write to null array");
175 break;
176 default: {
177 const DexFile& dex_file = Runtime::Current()->GetClassLinker()
178 ->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache());
179 std::string message("Null pointer exception during instruction '");
180 message += instr->DumpString(&dex_file);
181 message += "'";
182 self->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str());
183 break;
184 }
185 }
186 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700187}
188
189// Called by generated call to throw an arithmetic divide by zero exception
190extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700191 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700192 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
193 thread->DeliverException();
194}
195
196// Called by generated call to throw an arithmetic divide by zero exception
197extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700198 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
199 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
200 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700201 thread->DeliverException();
202}
203
204// Called by the AbstractMethodError stub (not runtime support)
205extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700206 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
207 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
208 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700209 thread->DeliverException();
210}
211
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700212extern "C" void artThrowStackOverflowFromCode(Method* /*method*/, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700213 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800214 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800215 if (Runtime::Current()->IsMethodTracingActive()) {
Elliott Hughes0ece7b92012-03-09 18:14:40 -0800216 TraceMethodUnwindFromCode(thread);
jeffhaoe343b762011-12-05 16:36:44 -0800217 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700218 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700219 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
220 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700221 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700222 thread->ResetDefaultStackEnd(); // Return to default stack size
223 thread->DeliverException();
224}
225
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700226static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700227 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700228 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
229 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
230
231 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700232 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700233 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
234 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700235 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700236 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
237 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700238 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700239 type_idx = ref;
240 } else {
241 CHECK(false) << static_cast<int>(ref_type);
242 }
243
Ian Rogers0571d352011-11-03 19:51:38 -0700244 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700245 if (!access) {
246 return class_name;
247 }
248
249 std::string result;
250 result += "tried to access class ";
251 result += class_name;
252 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800253 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700254 return result;
255}
256
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700257static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700258 verifier::VerifyErrorRefType ref_type, bool access) {
259 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700260
261 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
262 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
263
264 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700265 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700266 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700267 if (!access) {
268 return class_name + "." + field_name;
269 }
270
271 std::string result;
272 result += "tried to access field ";
273 result += class_name + "." + field_name;
274 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700276 return result;
277}
278
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700279static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800280 verifier::VerifyErrorRefType ref_type, bool access) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700281 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700282
283 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
284 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
285
286 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700287 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700288 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700289 if (!access) {
290 return class_name + "." + method_name;
291 }
292
293 std::string result;
294 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700295 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700296 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700297 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700299 return result;
300}
301
302extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700303 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
304 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700305 frame.Next();
306 Method* method = frame.GetMethod();
307
Ian Rogersd81871c2011-10-03 13:57:23 -0700308 verifier::VerifyErrorRefType ref_type =
309 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700310
311 const char* exception_class = "Ljava/lang/VerifyError;";
312 std::string msg;
313
Ian Rogersd81871c2011-10-03 13:57:23 -0700314 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
315 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700316 exception_class = "Ljava/lang/NoClassDefFoundError;";
317 msg = ClassNameFromIndex(method, ref, ref_type, false);
318 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700320 exception_class = "Ljava/lang/NoSuchFieldError;";
321 msg = FieldNameFromIndex(method, ref, ref_type, false);
322 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700323 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700324 exception_class = "Ljava/lang/NoSuchMethodError;";
325 msg = MethodNameFromIndex(method, ref, ref_type, false);
326 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700328 exception_class = "Ljava/lang/IllegalAccessError;";
329 msg = ClassNameFromIndex(method, ref, ref_type, true);
330 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700331 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700332 exception_class = "Ljava/lang/IllegalAccessError;";
333 msg = FieldNameFromIndex(method, ref, ref_type, true);
334 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700335 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700336 exception_class = "Ljava/lang/IllegalAccessError;";
337 msg = MethodNameFromIndex(method, ref, ref_type, true);
338 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700339 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700340 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
341 msg = ClassNameFromIndex(method, ref, ref_type, false);
342 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700343 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700344 exception_class = "Ljava/lang/InstantiationError;";
345 msg = ClassNameFromIndex(method, ref, ref_type, false);
346 break;
jeffhaod5347e02012-03-22 17:25:05 -0700347 case verifier::VERIFY_ERROR_BAD_CLASS_SOFT:
348 case verifier::VERIFY_ERROR_BAD_CLASS_HARD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700349 // Generic VerifyError; use default exception, no message.
350 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700351 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700352 CHECK(false);
353 break;
354 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700355 self->ThrowNewException(exception_class, msg.c_str());
356 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700357}
358
359extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700360 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700361 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700362 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700363 thread->DeliverException();
364}
365
366extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700367 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700368 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700369 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700370 thread->DeliverException();
371}
372
Elliott Hughese1410a22011-10-04 12:10:24 -0700373extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700374 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
375 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700376 frame.Next();
377 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700378 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700379 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700380 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700381}
382
383extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700384 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700385 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700386 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700387 thread->DeliverException();
388}
389
Ian Rogers19846512012-02-24 11:42:47 -0800390const void* UnresolvedDirectMethodTrampolineFromCode(Method* called, Method** sp, Thread* thread,
391 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700392 // TODO: this code is specific to ARM
393 // On entry the stack pointed by sp is:
394 // | argN | |
395 // | ... | |
396 // | arg4 | |
397 // | arg3 spill | | Caller's frame
398 // | arg2 spill | |
399 // | arg1 spill | |
400 // | Method* | ---
401 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700402 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700403 // | R3 | arg3
404 // | R2 | arg2
405 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700406 // | R0 |
407 // | Method* | <- sp
408 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
409 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
410 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
411 uintptr_t caller_pc = regs[10];
412 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700413 // Start new JNI local reference state
414 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700415 ScopedJniEnvLocalRefState env_state(env);
Ian Rogers19846512012-02-24 11:42:47 -0800416
417 // Compute details about the called method (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700418 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers19846512012-02-24 11:42:47 -0800419 Method* caller = *caller_sp;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700420 bool is_static;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800421 bool is_virtual;
Ian Rogers19846512012-02-24 11:42:47 -0800422 uint32_t dex_method_idx;
423 const char* shorty;
424 uint32_t shorty_len;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700425 if (type == Runtime::kUnknownMethod) {
Ian Rogers19846512012-02-24 11:42:47 -0800426 DCHECK(called->IsRuntimeMethod());
Ian Rogersdf9a7822011-10-11 16:53:22 -0700427 // less two as return address may span into next dex instruction
428 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700430 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
431 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700432 Instruction::Code instr_code = instr->Opcode();
433 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
434 (instr_code == Instruction::INVOKE_STATIC_RANGE);
Ian Rogersfb6adba2012-03-04 21:51:51 -0800435 is_virtual = (instr_code == Instruction::INVOKE_VIRTUAL) ||
Ian Rogers2ed3b952012-03-17 11:49:39 -0700436 (instr_code == Instruction::INVOKE_VIRTUAL_RANGE) ||
437 (instr_code == Instruction::INVOKE_SUPER) ||
438 (instr_code == Instruction::INVOKE_SUPER_RANGE);
439 DCHECK(is_static || is_virtual || (instr_code == Instruction::INVOKE_DIRECT) ||
440 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Elliott Hughesadb8c672012-03-06 16:49:32 -0800441 DecodedInstruction dec_insn(instr);
442 dex_method_idx = dec_insn.vB;
Ian Rogers19846512012-02-24 11:42:47 -0800443 shorty = linker->MethodShorty(dex_method_idx, caller, &shorty_len);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700444 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800445 DCHECK(!called->IsRuntimeMethod());
Ian Rogersea2a11d2011-10-11 16:48:51 -0700446 is_static = type == Runtime::kStaticMethod;
Ian Rogersfb6adba2012-03-04 21:51:51 -0800447 is_virtual = false;
Ian Rogers19846512012-02-24 11:42:47 -0800448 dex_method_idx = called->GetDexMethodIndex();
449 MethodHelper mh(called);
450 shorty = mh.GetShorty();
451 shorty_len = mh.GetShortyLength();
452 }
453 // Discover shorty (avoid GCs)
454 size_t args_in_regs = 0;
455 for (size_t i = 1; i < shorty_len; i++) {
456 char c = shorty[i];
457 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
458 if (args_in_regs > 3) {
459 args_in_regs = 3;
460 break;
461 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700462 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700463 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700464 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700465 if (!is_static) {
466 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
467 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700468 if (args_in_regs < 3) {
469 // If we thought we had fewer than 3 arguments in registers, account for the receiver
470 args_in_regs++;
471 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700472 AddLocalReference<jobject>(env, obj);
473 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700474 size_t shorty_index = 1; // skip return value
475 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
476 // R0)
477 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
478 char c = shorty[shorty_index];
479 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700480 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700481 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
482 AddLocalReference<jobject>(env, obj);
483 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700484 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
485 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700486 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700487 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700488 while (shorty_index < shorty_len) {
489 char c = shorty[shorty_index];
490 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700491 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700492 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700493 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700494 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700495 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700496 }
497 // Resolve method filling in dex cache
Ian Rogers19846512012-02-24 11:42:47 -0800498 if (type == Runtime::kUnknownMethod) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800499 called = linker->ResolveMethod(dex_method_idx, caller, !is_virtual);
Ian Rogers19846512012-02-24 11:42:47 -0800500 }
501 const void* code = NULL;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700502 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800503 if (LIKELY(called->IsDirect() == !is_virtual)) {
Ian Rogers19846512012-02-24 11:42:47 -0800504 // Ensure that the called method's class is initialized.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800505 Class* called_class = called->GetDeclaringClass();
506 linker->EnsureInitialized(called_class, true);
507 if (LIKELY(called_class->IsInitialized())) {
Ian Rogers19846512012-02-24 11:42:47 -0800508 code = called->GetCode();
509 } else if (called_class->IsInitializing()) {
Ian Rogers2ed3b952012-03-17 11:49:39 -0700510 if (is_static) {
511 // Class is still initializing, go to oat and grab code (trampoline must be left in place
512 // until class is initialized to stop races between threads).
513 code = linker->GetOatCodeFor(called);
514 } else {
515 // No trampoline for non-static methods.
516 code = called->GetCode();
517 }
Ian Rogers19846512012-02-24 11:42:47 -0800518 } else {
519 DCHECK(called_class->IsErroneous());
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800520 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800521 } else {
522 // Direct method has been made virtual
523 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
524 "Expected direct method but found virtual: %s",
525 PrettyMethod(called, true).c_str());
526 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700527 }
Ian Rogers19846512012-02-24 11:42:47 -0800528 if (UNLIKELY(code == NULL)) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700529 // Something went wrong in ResolveMethod or EnsureInitialized,
530 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700531 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700532 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700533 thread->ClearException();
534 } else {
Ian Rogers19846512012-02-24 11:42:47 -0800535 // Expect class to at least be initializing.
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800536 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogers19846512012-02-24 11:42:47 -0800537 // Don't want infinite recursion.
538 DCHECK(code != Runtime::Current()->GetResolutionStubArray(Runtime::kUnknownMethod)->GetData());
Ian Rogersad25ac52011-10-04 19:13:33 -0700539 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700540 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700541 }
542 return code;
543}
544
Ian Rogers60db5ab2012-02-20 17:02:00 -0800545static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
546 intptr_t value = *arg_ptr;
547 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
548 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
Elliott Hughes88c5c352012-03-15 18:49:48 -0700549 CHECK(Runtime::Current()->GetHeap()->IsHeapAddress(value_as_work_around_rep)) << value_as_work_around_rep;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800550 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
551}
552
553extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
554 DCHECK(Thread::Current() == self);
555 // TODO: this code is specific to ARM
556 // On entry the stack pointed by sp is:
557 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
558 // | LR |
559 // | R3 | arg2
560 // | R2 | arg1
561 // | R1 | jclass/jobject
562 // | R0 | JNIEnv
563 // | unused |
564 // | unused |
565 // | unused | <- sp
566 Method* jni_method = self->GetTopOfStack().GetMethod();
567 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
568 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
569 // Fix up this/jclass argument
570 WorkAroundJniBugsForJobject(arg_ptr);
571 arg_ptr++;
572 // Fix up jobject arguments
573 MethodHelper mh(jni_method);
574 int reg_num = 2; // Current register being processed, -1 for stack arguments.
Elliott Hughes45651fd2012-02-21 15:48:20 -0800575 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800576 char shorty_char = mh.GetShorty()[i];
577 if (shorty_char == 'L') {
578 WorkAroundJniBugsForJobject(arg_ptr);
579 }
580 if (shorty_char == 'J' || shorty_char == 'D') {
581 if (reg_num == 2) {
582 arg_ptr = sp + 8; // skip to out arguments
583 reg_num = -1;
584 } else if (reg_num == 3) {
585 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
586 reg_num = -1;
587 } else {
588 DCHECK(reg_num == -1);
589 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
590 arg_ptr += 3; // unaligned, pad and move through stack arguments
591 } else {
592 arg_ptr += 2; // aligned, move through stack arguments
593 }
594 }
595 } else {
596 if (reg_num == 2) {
597 arg_ptr++; // move through register arguments
598 reg_num++;
599 } else if (reg_num == 3) {
600 arg_ptr = sp + 8; // skip to outgoing stack arguments
601 reg_num = -1;
602 } else {
603 DCHECK(reg_num == -1);
604 arg_ptr++; // move through stack arguments
605 }
606 }
607 }
608 // Load expected destination, see Method::RegisterNative
Ian Rogers19846512012-02-24 11:42:47 -0800609 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
610 if (UNLIKELY(code == NULL)) {
611 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
612 jni_method->RegisterNative(self, code);
613 }
614 return code;
Ian Rogers60db5ab2012-02-20 17:02:00 -0800615}
616
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -0800617
618extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
619 Thread* self, Method** sp) {
620 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
621 if (LIKELY(field != NULL)) {
622 return field->Get32(NULL);
623 }
624 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
625 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int32_t));
626 if (LIKELY(field != NULL)) {
627 return field->Get32(NULL);
628 }
629 return 0; // Will throw exception by checking with Thread::Current
630}
631
632extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
633 Thread* self, Method** sp) {
634 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
635 if (LIKELY(field != NULL)) {
636 return field->Get64(NULL);
637 }
638 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
639 field = FindFieldFromCode(field_idx, referrer, self, true, true, false, sizeof(int64_t));
640 if (LIKELY(field != NULL)) {
641 return field->Get64(NULL);
642 }
643 return 0; // Will throw exception by checking with Thread::Current
644}
645
646extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
647 Thread* self, Method** sp) {
648 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
649 if (LIKELY(field != NULL)) {
650 return field->GetObj(NULL);
651 }
652 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
653 field = FindFieldFromCode(field_idx, referrer, self, true, false, false, sizeof(Object*));
654 if (LIKELY(field != NULL)) {
655 return field->GetObj(NULL);
656 }
657 return NULL; // Will throw exception by checking with Thread::Current
658}
659
660extern "C" uint32_t artGet32InstanceFromCode(uint32_t field_idx, Object* obj,
661 const Method* referrer, Thread* self, Method** sp) {
662 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int32_t));
663 if (LIKELY(field != NULL && obj != NULL)) {
664 return field->Get32(obj);
665 }
666 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
667 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int32_t));
668 if (LIKELY(field != NULL)) {
669 if (UNLIKELY(obj == NULL)) {
670 ThrowNullPointerExceptionForFieldAccess(self, field, true);
671 } else {
672 return field->Get32(obj);
673 }
674 }
675 return 0; // Will throw exception by checking with Thread::Current
676}
677
678extern "C" uint64_t artGet64InstanceFromCode(uint32_t field_idx, Object* obj,
679 const Method* referrer, Thread* self, Method** sp) {
680 Field* field = FindFieldFast(field_idx, referrer, true, false, sizeof(int64_t));
681 if (LIKELY(field != NULL && obj != NULL)) {
682 return field->Get64(obj);
683 }
684 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
685 field = FindFieldFromCode(field_idx, referrer, self, false, true, false, sizeof(int64_t));
686 if (LIKELY(field != NULL)) {
687 if (UNLIKELY(obj == NULL)) {
688 ThrowNullPointerExceptionForFieldAccess(self, field, true);
689 } else {
690 return field->Get64(obj);
691 }
692 }
693 return 0; // Will throw exception by checking with Thread::Current
694}
695
696extern "C" Object* artGetObjInstanceFromCode(uint32_t field_idx, Object* obj,
697 const Method* referrer, Thread* self, Method** sp) {
698 Field* field = FindFieldFast(field_idx, referrer, false, false, sizeof(Object*));
699 if (LIKELY(field != NULL && obj != NULL)) {
700 return field->GetObj(obj);
701 }
702 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
703 field = FindFieldFromCode(field_idx, referrer, self, false, false, false, sizeof(Object*));
704 if (LIKELY(field != NULL)) {
705 if (UNLIKELY(obj == NULL)) {
706 ThrowNullPointerExceptionForFieldAccess(self, field, true);
707 } else {
708 return field->GetObj(obj);
709 }
710 }
711 return NULL; // Will throw exception by checking with Thread::Current
712}
713
714extern "C" int artSet32StaticFromCode(uint32_t field_idx, uint32_t new_value,
715 const Method* referrer, Thread* self, Method** sp) {
716 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
717 if (LIKELY(field != NULL)) {
718 field->Set32(NULL, new_value);
719 return 0; // success
720 }
721 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
722 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int32_t));
723 if (LIKELY(field != NULL)) {
724 field->Set32(NULL, new_value);
725 return 0; // success
726 }
727 return -1; // failure
728}
729
730extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
731 uint64_t new_value, Thread* self, Method** sp) {
732 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
733 if (LIKELY(field != NULL)) {
734 field->Set64(NULL, new_value);
735 return 0; // success
736 }
737 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
738 field = FindFieldFromCode(field_idx, referrer, self, true, true, true, sizeof(int64_t));
739 if (LIKELY(field != NULL)) {
740 field->Set64(NULL, new_value);
741 return 0; // success
742 }
743 return -1; // failure
744}
745
746extern "C" int artSetObjStaticFromCode(uint32_t field_idx, Object* new_value,
747 const Method* referrer, Thread* self, Method** sp) {
748 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
749 if (LIKELY(field != NULL)) {
750 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
751 field->SetObj(NULL, new_value);
752 return 0; // success
753 }
754 }
755 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
756 field = FindFieldFromCode(field_idx, referrer, self, true, false, true, sizeof(Object*));
757 if (LIKELY(field != NULL)) {
758 field->SetObj(NULL, new_value);
759 return 0; // success
760 }
761 return -1; // failure
762}
763
764extern "C" int artSet32InstanceFromCode(uint32_t field_idx, Object* obj, uint32_t new_value,
765 const Method* referrer, Thread* self, Method** sp) {
766 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int32_t));
767 if (LIKELY(field != NULL && obj != NULL)) {
768 field->Set32(obj, new_value);
769 return 0; // success
770 }
771 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
772 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int32_t));
773 if (LIKELY(field != NULL)) {
774 if (UNLIKELY(obj == NULL)) {
775 ThrowNullPointerExceptionForFieldAccess(self, field, false);
776 } else {
777 field->Set32(obj, new_value);
778 return 0; // success
779 }
780 }
781 return -1; // failure
782}
783
784extern "C" int artSet64InstanceFromCode(uint32_t field_idx, Object* obj, uint64_t new_value,
785 Thread* self, Method** sp) {
786 Method* callee_save = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsOnly);
787 Method* referrer = sp[callee_save->GetFrameSizeInBytes() / sizeof(Method*)];
788 Field* field = FindFieldFast(field_idx, referrer, true, true, sizeof(int64_t));
789 if (LIKELY(field != NULL && obj != NULL)) {
790 field->Set64(obj, new_value);
791 return 0; // success
792 }
793 *sp = callee_save;
794 self->SetTopOfStack(sp, 0);
795 field = FindFieldFromCode(field_idx, referrer, self, false, true, true, sizeof(int64_t));
796 if (LIKELY(field != NULL)) {
797 if (UNLIKELY(obj == NULL)) {
798 ThrowNullPointerExceptionForFieldAccess(self, field, false);
799 } else {
800 field->Set64(obj, new_value);
801 return 0; // success
802 }
803 }
804 return -1; // failure
805}
806
807extern "C" int artSetObjInstanceFromCode(uint32_t field_idx, Object* obj, Object* new_value,
808 const Method* referrer, Thread* self, Method** sp) {
809 Field* field = FindFieldFast(field_idx, referrer, false, true, sizeof(Object*));
810 if (LIKELY(field != NULL && obj != NULL)) {
811 field->SetObj(obj, new_value);
812 return 0; // success
813 }
814 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
815 field = FindFieldFromCode(field_idx, referrer, self, false, false, true, sizeof(Object*));
816 if (LIKELY(field != NULL)) {
817 if (UNLIKELY(obj == NULL)) {
818 ThrowNullPointerExceptionForFieldAccess(self, field, false);
819 } else {
820 field->SetObj(obj, new_value);
821 return 0; // success
822 }
823 }
824 return -1; // failure
825}
826
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800827extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
828 Thread* self, Method** sp) {
829 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
830 return AllocObjectFromCode(type_idx, method, self, false);
831}
832
Ian Rogers28ad40d2011-10-27 15:19:26 -0700833extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
834 Thread* self, Method** sp) {
835 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800836 return AllocObjectFromCode(type_idx, method, self, true);
837}
838
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800839extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
840 Thread* self, Method** sp) {
841 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
842 return AllocArrayFromCode(type_idx, method, component_count, self, false);
843}
844
845extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
846 int32_t component_count,
847 Thread* self, Method** sp) {
848 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
849 return AllocArrayFromCode(type_idx, method, component_count, self, true);
850}
851
Ian Rogersce9eca62011-10-07 17:11:03 -0700852extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
853 int32_t component_count, Thread* self, Method** sp) {
854 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800855 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700856}
857
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800858extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
859 int32_t component_count,
860 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700861 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800862 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700863}
864
Ian Rogerscaab8c42011-10-12 12:11:18 -0700865// Assignable test for code, won't throw. Null and equality tests already performed
866uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
867 DCHECK(klass != NULL);
868 DCHECK(ref_class != NULL);
869 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
870}
871
Shih-wei Liao2d831012011-09-28 22:06:53 -0700872// 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 -0700873extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700874 DCHECK(a->IsClass()) << PrettyClass(a);
875 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700876 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700877 return 0; // Success
878 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700879 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700880 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700881 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800882 PrettyDescriptor(a).c_str(),
883 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700884 return -1; // Failure
885 }
886}
887
888// Tests whether 'element' can be assigned into an array of type 'array_class'.
889// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700890extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
891 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700892 DCHECK(array_class != NULL);
893 // element can't be NULL as we catch this is screened in runtime_support
894 Class* element_class = element->GetClass();
895 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700896 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700897 return 0; // Success
898 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700899 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700900 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800901 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800902 PrettyDescriptor(element_class).c_str(),
903 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700904 return -1; // Failure
905 }
906}
907
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700908extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
909 Thread* self, Method** sp) {
910 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800911 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700912}
913
Ian Rogers28ad40d2011-10-27 15:19:26 -0700914extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
915 Method** sp) {
916 // Called when method->dex_cache_resolved_types_[] misses
917 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800918 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700919}
920
Ian Rogersb093c6b2011-10-31 16:19:55 -0700921extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
922 const Method* referrer, Thread* self,
923 Method** sp) {
924 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
925 // unpopulated
926 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800927 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700928}
929
Brian Carlstromaded5f72011-10-07 17:15:04 -0700930extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
931 Thread* self, Method** sp) {
932 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
933 return ResolveStringFromCode(referrer, string_idx);
934}
935
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700936extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
937 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700938 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700939 // MonitorExit may throw exception
940 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
941}
942
943extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
944 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
945 DCHECK(obj != NULL); // Assumed to have been checked before entry
946 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700947 DCHECK(thread->HoldsLock(obj));
948 // Only possible exception is NPE and is handled before entry
949 DCHECK(!thread->IsExceptionPending());
950}
951
Ian Rogers4a510d82011-10-09 14:30:24 -0700952void CheckSuspendFromCode(Thread* thread) {
953 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700954 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
955}
956
Ian Rogers4a510d82011-10-09 14:30:24 -0700957extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
958 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700959 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700960 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
961}
962
963/*
964 * Fill the array with predefined constant values, throwing exceptions if the array is null or
965 * not of sufficient length.
966 *
967 * NOTE: When dealing with a raw dex file, the data to be copied uses
968 * little-endian ordering. Require that oat2dex do any required swapping
969 * so this routine can get by with a memcpy().
970 *
971 * Format of the data:
972 * ushort ident = 0x0300 magic value
973 * ushort width width of each element in the table
974 * uint size number of elements in the table
975 * ubyte data[size*width] table of data values (may contain a single-byte
976 * padding at the end)
977 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700978extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
979 Thread* self, Method** sp) {
980 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700981 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700982 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700983 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
984 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700985 return -1; // Error
986 }
987 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
988 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700989 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700990 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
991 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700992 return -1; // Error
993 }
994 uint16_t width = table[1];
995 uint32_t size_in_bytes = size * width;
Ian Rogersa15e67d2012-02-28 13:51:55 -0800996 memcpy((char*)array + Array::DataOffset(width).Int32Value(), (char*)&table[4], size_in_bytes);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700997 return 0; // Success
998}
999
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -08001000static uint64_t artInvokeCommon(uint32_t method_idx, Object* this_object, Method* caller_method,
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001001 Thread* self, Method** sp, bool access_check, InvokeType type) {
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -08001002 Method* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
1003 if (UNLIKELY(method == NULL)) {
1004 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1005 if (UNLIKELY(this_object == NULL && type != kDirect && type != kStatic)) {
1006 ThrowNullPointerExceptionForMethodAccess(self, caller_method, method_idx, type);
1007 return 0; // failure
1008 }
1009 method = FindMethodFromCode(method_idx, this_object, caller_method, self, access_check, type);
1010 if (UNLIKELY(method == NULL)) {
1011 CHECK(self->IsExceptionPending());
1012 return 0; // failure
1013 }
1014 }
1015 DCHECK(!self->IsExceptionPending());
1016 const void* code = method->GetCode();
1017
Elliott Hughes634eb2e2012-03-22 16:06:28 -07001018 // When we return, the caller will branch to this address, so it had better not be 0!
1019 CHECK(code != NULL) << PrettyMethod(method);
1020
Shih-wei Liaoddbd01a2012-03-09 14:42:12 -08001021 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
1022 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1023 uint64_t result = ((code_uint << 32) | method_uint);
1024 return result;
1025}
1026
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001027// See comments in runtime_support_asm.S
1028extern "C" uint64_t artInvokeInterfaceTrampoline(uint32_t method_idx, Object* this_object,
1029 Method* caller_method, Thread* self,
1030 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001031 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, false, kInterface);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001032}
1033
1034extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1035 Object* this_object,
1036 Method* caller_method, Thread* self,
1037 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001038 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kInterface);
1039}
1040
1041
1042extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1043 Object* this_object,
1044 Method* caller_method, Thread* self,
1045 Method** sp) {
1046 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kDirect);
1047}
1048
1049extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1050 Object* this_object,
1051 Method* caller_method, Thread* self,
1052 Method** sp) {
1053 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kStatic);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001054}
1055
1056extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1057 Object* this_object,
1058 Method* caller_method, Thread* self,
1059 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001060 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kSuper);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001061}
1062
1063extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1064 Object* this_object,
1065 Method* caller_method, Thread* self,
1066 Method** sp) {
Ian Rogersc8b306f2012-02-17 21:34:44 -08001067 return artInvokeCommon(method_idx, this_object, caller_method, self, sp, true, kVirtual);
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001068}
1069
Ian Rogers466bb252011-10-14 03:29:56 -07001070static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1071 ScopedLocalRef<jclass> jlr_UTE_class(env,
1072 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1073 if (jlr_UTE_class.get() == NULL) {
1074 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1075 } else {
1076 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1077 "(Ljava/lang/Throwable;)V");
1078 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1079 ScopedLocalRef<jthrowable> jlr_UTE(env,
1080 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1081 jexception)));
1082 int rc = env->Throw(jlr_UTE.get());
1083 if (rc != JNI_OK) {
1084 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1085 }
1086 }
1087 CHECK(self->IsExceptionPending());
1088}
1089
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001090// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1091// which is responsible for recording callee save registers. We explicitly handlerize incoming
1092// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1093// the invocation handler which is a field within the proxy object receiver.
1094extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001095 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001096 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001097 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1098 DCHECK_EQ(*proxy_sp, proxy_method);
1099 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001100 // TODO: ARM specific
1101 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1102 // Start new JNI local reference state
1103 JNIEnvExt* env = self->GetJniEnv();
1104 ScopedJniEnvLocalRefState env_state(env);
1105 // Create local ref. copies of proxy method and the receiver
1106 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1107 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1108
Ian Rogers14b1b242011-10-11 18:54:34 -07001109 // Placing into local references incoming arguments from the caller's register arguments,
1110 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001111 MethodHelper proxy_mh(proxy_method);
1112 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001113 size_t args_in_regs = 0;
1114 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001115 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001116 if (args_in_regs > 2) {
1117 args_in_regs = 2;
1118 break;
1119 }
1120 }
1121 size_t cur_arg = 0; // current stack location to read
1122 size_t param_index = 1; // skip receiver
1123 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001124 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001125 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001126 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001127 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001128 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001129 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001130 param_index++;
1131 }
1132 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001133 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001134 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001135 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001136 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1137 jobject jobj = AddLocalReference<jobject>(env, obj);
1138 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001139 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001140 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001141 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001142 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001143 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1144 jvalue args_jobj[3];
1145 args_jobj[0].l = rcvr_jobj;
1146 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001147 // Args array, if no arguments then NULL (don't include receiver in argument count)
1148 args_jobj[2].l = NULL;
1149 ObjectArray<Object>* args = NULL;
1150 if ((num_params - 1) > 0) {
1151 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001152 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001153 CHECK(self->IsExceptionPending());
1154 return;
1155 }
1156 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1157 }
1158 // Convert proxy method into expected interface method
1159 Method* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers19846512012-02-24 11:42:47 -08001160 DCHECK(interface_method != NULL);
1161 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001162 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001163 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001164 cur_arg = 0; // reset stack location to read to start
1165 // reset index, will index into param type array which doesn't include the receiver
1166 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001167 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
jeffhao441d9122012-03-21 17:29:10 -07001168 if (param_types == NULL) {
1169 CHECK(self->IsExceptionPending());
1170 return;
1171 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001172 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
Ian Rogers19846512012-02-24 11:42:47 -08001173 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001174 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1175 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001176 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001177 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001178 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001179 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001180 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1181 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1182 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001183 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001184 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001185 }
Elliott Hughesdbac3092012-03-16 18:00:30 -07001186 BoxPrimitive(param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001187 if (self->IsExceptionPending()) {
1188 return;
1189 }
1190 obj = val.l;
1191 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001192 args->Set(param_index, obj);
1193 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1194 param_index++;
1195 }
1196 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001197 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1198 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001199 Class* param_type = param_types->Get(param_index);
1200 Object* obj;
1201 if (!param_type->IsPrimitive()) {
1202 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1203 } else {
1204 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Elliott Hughesdbac3092012-03-16 18:00:30 -07001205 BoxPrimitive(param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001206 if (self->IsExceptionPending()) {
1207 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001208 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001209 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001210 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001211 args->Set(param_index, obj);
1212 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1213 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001214 }
1215 // Get the InvocationHandler method and the field that holds it within the Proxy object
1216 static jmethodID inv_hand_invoke_mid = NULL;
1217 static jfieldID proxy_inv_hand_fid = NULL;
1218 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001219 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001220 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001221 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001222 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1223 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1224 }
Ian Rogers466bb252011-10-14 03:29:56 -07001225 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001226 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1227 // Call InvocationHandler.invoke
1228 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1229 // Place result in stack args
1230 if (!self->IsExceptionPending()) {
1231 Object* result_ref = self->DecodeJObject(result);
1232 if (result_ref != NULL) {
1233 JValue result_unboxed;
Elliott Hughesdbac3092012-03-16 18:00:30 -07001234 bool unboxed_okay = UnboxPrimitive(result_ref, proxy_mh.GetReturnType(), result_unboxed, "result");
Elliott Hughes051c9fc2012-03-21 12:24:55 -07001235 if (!unboxed_okay) {
1236 self->ClearException();
1237 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
1238 "Couldn't convert result of type %s to %s",
1239 PrettyTypeOf(result_ref).c_str(),
1240 PrettyDescriptor(proxy_mh.GetReturnType()).c_str());
1241 return;
1242 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001243 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1244 } else {
1245 *reinterpret_cast<jobject*>(stack_args) = NULL;
1246 }
Ian Rogers466bb252011-10-14 03:29:56 -07001247 } else {
1248 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1249 // a UndeclaredThrowableException.
1250 Throwable* exception = self->GetException();
1251 self->ClearException();
1252 if (!exception->IsCheckedException()) {
1253 self->SetException(exception);
1254 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001255 SynthesizedProxyClass* proxy_class =
1256 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1257 int throws_index = -1;
1258 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1259 for (size_t i = 0; i < num_virt_methods; i++) {
1260 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1261 throws_index = i;
1262 break;
1263 }
1264 }
1265 CHECK_NE(throws_index, -1);
1266 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001267 Class* exception_class = exception->GetClass();
1268 bool declares_exception = false;
1269 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1270 Class* declared_exception = declared_exceptions->Get(i);
1271 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1272 }
1273 if (declares_exception) {
1274 self->SetException(exception);
1275 } else {
1276 ThrowNewUndeclaredThrowableException(self, env, exception);
1277 }
1278 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001279 }
1280}
1281
jeffhaoe343b762011-12-05 16:36:44 -08001282extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001283 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001284 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1285 self->PushTraceStackFrame(trace_frame);
1286
jeffhao2692b572011-12-16 15:42:28 -08001287 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001288
jeffhao2692b572011-12-16 15:42:28 -08001289 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001290}
1291
1292extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001293 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001294 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1295 Method* method = trace_frame.method_;
1296 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001297
jeffhao2692b572011-12-16 15:42:28 -08001298 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001299
1300 return lr;
1301}
1302
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001303uint32_t TraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001304 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001305 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1306 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001307 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001308
jeffhao2692b572011-12-16 15:42:28 -08001309 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001310
1311 return lr;
1312}
1313
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001314int CmplFloat(float a, float b) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001315 if (a == b) {
1316 return 0;
1317 } else if (a < b) {
1318 return -1;
1319 } else if (a > b) {
1320 return 1;
1321 }
1322 return -1;
1323}
1324
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001325int CmpgFloat(float a, float b) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001326 if (a == b) {
1327 return 0;
1328 } else if (a < b) {
1329 return -1;
1330 } else if (a > b) {
1331 return 1;
1332 }
1333 return 1;
1334}
1335
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001336int CmpgDouble(double a, double b) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001337 if (a == b) {
1338 return 0;
1339 } else if (a < b) {
1340 return -1;
1341 } else if (a > b) {
1342 return 1;
1343 }
1344 return 1;
1345}
1346
Elliott Hughes0ece7b92012-03-09 18:14:40 -08001347int CmplDouble(double a, double b) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001348 if (a == b) {
1349 return 0;
1350 } else if (a < b) {
1351 return -1;
1352 } else if (a > b) {
1353 return 1;
1354 }
1355 return -1;
1356}
1357
Shih-wei Liao2d831012011-09-28 22:06:53 -07001358/*
1359 * Float/double conversion requires clamping to min and max of integer form. If
1360 * target doesn't support this normally, use these.
1361 */
1362int64_t D2L(double d) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001363 static const double kMaxLong = (double) (int64_t) 0x7fffffffffffffffULL;
1364 static const double kMinLong = (double) (int64_t) 0x8000000000000000ULL;
1365 if (d >= kMaxLong) {
1366 return (int64_t) 0x7fffffffffffffffULL;
1367 } else if (d <= kMinLong) {
1368 return (int64_t) 0x8000000000000000ULL;
1369 } else if (d != d) { // NaN case
1370 return 0;
1371 } else {
1372 return (int64_t) d;
1373 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001374}
1375
1376int64_t F2L(float f) {
Ian Rogersa433b2e2012-03-09 08:40:33 -08001377 static const float kMaxLong = (float) (int64_t) 0x7fffffffffffffffULL;
1378 static const float kMinLong = (float) (int64_t) 0x8000000000000000ULL;
1379 if (f >= kMaxLong) {
1380 return (int64_t) 0x7fffffffffffffffULL;
1381 } else if (f <= kMinLong) {
1382 return (int64_t) 0x8000000000000000ULL;
1383 } else if (f != f) { // NaN case
1384 return 0;
1385 } else {
1386 return (int64_t) f;
1387 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001388}
1389
1390} // namespace art