blob: 63efbf738cde31439e69dc2d425bc52a02dbb2a4 [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
2 * Copyright 2011 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "runtime_support.h"
18
Ian Rogerscaab8c42011-10-12 12:11:18 -070019#include "dex_cache.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070020#include "dex_verifier.h"
Ian Rogerscaab8c42011-10-12 12:11:18 -070021#include "macros.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object.h"
23#include "object_utils.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070024#include "reflection.h"
jeffhaoe343b762011-12-05 16:36:44 -080025#include "trace.h"
Ian Rogersdfcdf1a2011-10-10 17:50:35 -070026#include "ScopedLocalRef.h"
Elliott Hughes6c8867d2011-10-03 16:34:05 -070027
Shih-wei Liao2d831012011-09-28 22:06:53 -070028namespace art {
29
Ian Rogers4f0d07c2011-10-06 23:38:47 -070030// Place a special frame at the TOS that will save the callee saves for the given type
31static void FinishCalleeSaveFrameSetup(Thread* self, Method** sp, Runtime::CalleeSaveType type) {
Ian Rogersce9eca62011-10-07 17:11:03 -070032 // Be aware the store below may well stomp on an incoming argument
Ian Rogers4f0d07c2011-10-06 23:38:47 -070033 *sp = Runtime::Current()->GetCalleeSaveMethod(type);
34 self->SetTopOfStack(sp, 0);
35}
36
Shih-wei Liao2d831012011-09-28 22:06:53 -070037// Temporary debugging hook for compiler.
38extern void DebugMe(Method* method, uint32_t info) {
39 LOG(INFO) << "DebugMe";
40 if (method != NULL) {
41 LOG(INFO) << PrettyMethod(method);
42 }
43 LOG(INFO) << "Info: " << info;
44}
45
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -070046extern "C" uint32_t artObjectInitFromCode(Object* o, Thread* self, Method** sp) {
47 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -070048 Class* c = o->GetClass();
49 if (UNLIKELY(c->IsFinalizable())) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -070050 Heap::AddFinalizerReference(self, o);
Ian Rogerscaab8c42011-10-12 12:11:18 -070051 }
52 /*
53 * NOTE: once debugger/profiler support is added, we'll need to check
54 * here and branch to actual compiled object.<init> to handle any
Ian Rogers0571d352011-11-03 19:51:38 -070055 * breakpoint/logging activities if either is active.
Ian Rogerscaab8c42011-10-12 12:11:18 -070056 */
Brian Carlstrom6fd03fb2011-10-17 16:11:00 -070057 return self->IsExceptionPending() ? -1 : 0;
Ian Rogerscaab8c42011-10-12 12:11:18 -070058}
59
Shih-wei Liao2d831012011-09-28 22:06:53 -070060// 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
68extern void* FindNativeMethod(Thread* thread) {
69 DCHECK(Thread::Current() == thread);
70
71 Method* method = const_cast<Method*>(thread->GetCurrentMethod());
72 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.
76 void* native_code = thread->GetJniEnv()->vm->FindCodeForNativeMethod(method);
77 if (native_code == NULL) {
78 DCHECK(thread->IsExceptionPending());
79 return NULL;
80 } else {
81 // Register so that future calls don't come here
82 method->RegisterNative(native_code);
83 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
112extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700113 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700114 thread->ThrowNewException("Ljava/lang/NullPointerException;", NULL);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700115 thread->DeliverException();
116}
117
118// Called by generated call to throw an arithmetic divide by zero exception
119extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700120 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700121 thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero");
122 thread->DeliverException();
123}
124
125// Called by generated call to throw an arithmetic divide by zero exception
126extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700127 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
128 thread->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
129 "length=%d; index=%d", limit, index);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700130 thread->DeliverException();
131}
132
133// Called by the AbstractMethodError stub (not runtime support)
134extern void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700135 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
136 thread->ThrowNewExceptionF("Ljava/lang/AbstractMethodError;",
137 "abstract method \"%s\"", PrettyMethod(method).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700138 thread->DeliverException();
139}
140
141extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700142 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
jeffhaoe343b762011-12-05 16:36:44 -0800143 // Remove extra entry pushed onto second stack during method tracing
jeffhao2692b572011-12-16 15:42:28 -0800144 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhaoe343b762011-12-05 16:36:44 -0800145 artTraceMethodUnwindFromCode(thread);
146 }
Shih-wei Liao2d831012011-09-28 22:06:53 -0700147 thread->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700148 thread->ThrowNewExceptionF("Ljava/lang/StackOverflowError;",
149 "stack size %zdkb; default stack size: %zdkb",
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700150 thread->GetStackSize() / KB, Runtime::Current()->GetDefaultStackSize() / KB);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700151 thread->ResetDefaultStackEnd(); // Return to default stack size
152 thread->DeliverException();
153}
154
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700155static std::string ClassNameFromIndex(Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700156 verifier::VerifyErrorRefType ref_type, bool access) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700157 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
158 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
159
160 uint16_t type_idx = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700161 if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700162 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
163 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700164 } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700165 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
166 type_idx = id.class_idx_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700167 } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) {
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700168 type_idx = ref;
169 } else {
170 CHECK(false) << static_cast<int>(ref_type);
171 }
172
Ian Rogers0571d352011-11-03 19:51:38 -0700173 std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx)));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700174 if (!access) {
175 return class_name;
176 }
177
178 std::string result;
179 result += "tried to access class ";
180 result += class_name;
181 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800182 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700183 return result;
184}
185
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700186static std::string FieldNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700187 verifier::VerifyErrorRefType ref_type, bool access) {
188 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700189
190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
191 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
192
193 const DexFile::FieldId& id = dex_file.GetFieldId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700194 std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700195 const char* field_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700196 if (!access) {
197 return class_name + "." + field_name;
198 }
199
200 std::string result;
201 result += "tried to access field ";
202 result += class_name + "." + field_name;
203 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800204 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700205 return result;
206}
207
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700208static std::string MethodNameFromIndex(const Method* method, uint32_t ref,
Ian Rogersd81871c2011-10-03 13:57:23 -0700209 verifier::VerifyErrorRefType ref_type, bool access) {
210 CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD));
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700211
212 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
213 const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache());
214
215 const DexFile::MethodId& id = dex_file.GetMethodId(ref);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700216 std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id)));
Ian Rogers0571d352011-11-03 19:51:38 -0700217 const char* method_name = dex_file.StringDataByIdx(id.name_idx_);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700218 if (!access) {
219 return class_name + "." + method_name;
220 }
221
222 std::string result;
223 result += "tried to access method ";
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700224 result += class_name + "." + method_name + ":" +
Ian Rogers0571d352011-11-03 19:51:38 -0700225 dex_file.CreateMethodSignature(id.proto_idx_, NULL);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700226 result += " from class ";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227 result += PrettyDescriptor(method->GetDeclaringClass());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700228 return result;
229}
230
231extern "C" void artThrowVerificationErrorFromCode(int32_t kind, int32_t ref, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700232 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
233 Frame frame = self->GetTopOfStack(); // We need the calling method as context to interpret 'ref'
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700234 frame.Next();
235 Method* method = frame.GetMethod();
236
Ian Rogersd81871c2011-10-03 13:57:23 -0700237 verifier::VerifyErrorRefType ref_type =
238 static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700239
240 const char* exception_class = "Ljava/lang/VerifyError;";
241 std::string msg;
242
Ian Rogersd81871c2011-10-03 13:57:23 -0700243 switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) {
244 case verifier::VERIFY_ERROR_NO_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700245 exception_class = "Ljava/lang/NoClassDefFoundError;";
246 msg = ClassNameFromIndex(method, ref, ref_type, false);
247 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700248 case verifier::VERIFY_ERROR_NO_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700249 exception_class = "Ljava/lang/NoSuchFieldError;";
250 msg = FieldNameFromIndex(method, ref, ref_type, false);
251 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700252 case verifier::VERIFY_ERROR_NO_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700253 exception_class = "Ljava/lang/NoSuchMethodError;";
254 msg = MethodNameFromIndex(method, ref, ref_type, false);
255 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700256 case verifier::VERIFY_ERROR_ACCESS_CLASS:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700257 exception_class = "Ljava/lang/IllegalAccessError;";
258 msg = ClassNameFromIndex(method, ref, ref_type, true);
259 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700260 case verifier::VERIFY_ERROR_ACCESS_FIELD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700261 exception_class = "Ljava/lang/IllegalAccessError;";
262 msg = FieldNameFromIndex(method, ref, ref_type, true);
263 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700264 case verifier::VERIFY_ERROR_ACCESS_METHOD:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700265 exception_class = "Ljava/lang/IllegalAccessError;";
266 msg = MethodNameFromIndex(method, ref, ref_type, true);
267 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700268 case verifier::VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700269 exception_class = "Ljava/lang/IncompatibleClassChangeError;";
270 msg = ClassNameFromIndex(method, ref, ref_type, false);
271 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700272 case verifier::VERIFY_ERROR_INSTANTIATION:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700273 exception_class = "Ljava/lang/InstantiationError;";
274 msg = ClassNameFromIndex(method, ref, ref_type, false);
275 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700276 case verifier::VERIFY_ERROR_GENERIC:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700277 // Generic VerifyError; use default exception, no message.
278 break;
Ian Rogersd81871c2011-10-03 13:57:23 -0700279 case verifier::VERIFY_ERROR_NONE:
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700280 CHECK(false);
281 break;
282 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700283 self->ThrowNewException(exception_class, msg.c_str());
284 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700285}
286
287extern "C" void artThrowInternalErrorFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700288 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700289 LOG(WARNING) << "TODO: internal error detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700290 thread->ThrowNewExceptionF("Ljava/lang/InternalError;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700291 thread->DeliverException();
292}
293
294extern "C" void artThrowRuntimeExceptionFromCode(int32_t errnum, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700295 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700296 LOG(WARNING) << "TODO: runtime exception detail message. errnum=" << errnum;
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700297 thread->ThrowNewExceptionF("Ljava/lang/RuntimeException;", "errnum=%d", errnum);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700298 thread->DeliverException();
299}
300
Elliott Hughese1410a22011-10-04 12:10:24 -0700301extern "C" void artThrowNoSuchMethodFromCode(int32_t method_idx, Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700302 FinishCalleeSaveFrameSetup(self, sp, Runtime::kSaveAll);
303 Frame frame = self->GetTopOfStack(); // We need the calling method as context for the method_idx
Elliott Hughese1410a22011-10-04 12:10:24 -0700304 frame.Next();
305 Method* method = frame.GetMethod();
Elliott Hughese1410a22011-10-04 12:10:24 -0700306 self->ThrowNewException("Ljava/lang/NoSuchMethodError;",
Ian Rogersd81871c2011-10-03 13:57:23 -0700307 MethodNameFromIndex(method, method_idx, verifier::VERIFY_ERROR_REF_METHOD, false).c_str());
Elliott Hughese1410a22011-10-04 12:10:24 -0700308 self->DeliverException();
Shih-wei Liao2d831012011-09-28 22:06:53 -0700309}
310
311extern "C" void artThrowNegArraySizeFromCode(int32_t size, Thread* thread, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700312 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kSaveAll);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700313 LOG(WARNING) << "UNTESTED artThrowNegArraySizeFromCode";
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700314 thread->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700315 thread->DeliverException();
316}
317
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700318void* UnresolvedDirectMethodTrampolineFromCode(int32_t method_idx, Method** sp, Thread* thread,
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700319 Runtime::TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700320 // TODO: this code is specific to ARM
321 // On entry the stack pointed by sp is:
322 // | argN | |
323 // | ... | |
324 // | arg4 | |
325 // | arg3 spill | | Caller's frame
326 // | arg2 spill | |
327 // | arg1 spill | |
328 // | Method* | ---
329 // | LR |
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700330 // | ... | callee saves
Ian Rogersad25ac52011-10-04 19:13:33 -0700331 // | R3 | arg3
332 // | R2 | arg2
333 // | R1 | arg1
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700334 // | R0 |
335 // | Method* | <- sp
336 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
337 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
338 Method** caller_sp = reinterpret_cast<Method**>(reinterpret_cast<byte*>(sp) + 48);
339 uintptr_t caller_pc = regs[10];
340 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
Ian Rogersad25ac52011-10-04 19:13:33 -0700341 // Start new JNI local reference state
342 JNIEnvExt* env = thread->GetJniEnv();
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700343 ScopedJniEnvLocalRefState env_state(env);
Ian Rogersad25ac52011-10-04 19:13:33 -0700344 // Discover shorty (avoid GCs)
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700345 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogersad25ac52011-10-04 19:13:33 -0700346 const char* shorty = linker->MethodShorty(method_idx, *caller_sp);
347 size_t shorty_len = strlen(shorty);
Ian Rogers14b1b242011-10-11 18:54:34 -0700348 size_t args_in_regs = 0;
349 for (size_t i = 1; i < shorty_len; i++) {
350 char c = shorty[i];
351 args_in_regs = args_in_regs + (c == 'J' || c == 'D' ? 2 : 1);
352 if (args_in_regs > 3) {
353 args_in_regs = 3;
354 break;
355 }
356 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700357 bool is_static;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700358 if (type == Runtime::kUnknownMethod) {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700359 Method* caller = *caller_sp;
Ian Rogersdf9a7822011-10-11 16:53:22 -0700360 // less two as return address may span into next dex instruction
361 uint32_t dex_pc = caller->ToDexPC(caller_pc - 2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800362 const DexFile::CodeItem* code = MethodHelper(caller).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -0700363 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
364 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700365 Instruction::Code instr_code = instr->Opcode();
366 is_static = (instr_code == Instruction::INVOKE_STATIC) ||
367 (instr_code == Instruction::INVOKE_STATIC_RANGE);
368 DCHECK(is_static || (instr_code == Instruction::INVOKE_DIRECT) ||
369 (instr_code == Instruction::INVOKE_DIRECT_RANGE));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700370 } else {
Ian Rogersea2a11d2011-10-11 16:48:51 -0700371 is_static = type == Runtime::kStaticMethod;
372 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700373 // Place into local references incoming arguments from the caller's register arguments
Ian Rogers14b1b242011-10-11 18:54:34 -0700374 size_t cur_arg = 1; // skip method_idx in R0, first arg is in R1
Ian Rogersea2a11d2011-10-11 16:48:51 -0700375 if (!is_static) {
376 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
377 cur_arg++;
Ian Rogers14b1b242011-10-11 18:54:34 -0700378 if (args_in_regs < 3) {
379 // If we thought we had fewer than 3 arguments in registers, account for the receiver
380 args_in_regs++;
381 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700382 AddLocalReference<jobject>(env, obj);
383 }
Ian Rogers14b1b242011-10-11 18:54:34 -0700384 size_t shorty_index = 1; // skip return value
385 // Iterate while arguments and arguments in registers (less 1 from cur_arg which is offset to skip
386 // R0)
387 while ((cur_arg - 1) < args_in_regs && shorty_index < shorty_len) {
388 char c = shorty[shorty_index];
389 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700390 if (c == 'L') {
Ian Rogersad25ac52011-10-04 19:13:33 -0700391 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
392 AddLocalReference<jobject>(env, obj);
393 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700394 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
395 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700396 // Place into local references incoming arguments from the caller's stack arguments
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700397 cur_arg += 11; // skip LR, Method* and spills for R1 to R3 and callee saves
Ian Rogers14b1b242011-10-11 18:54:34 -0700398 while (shorty_index < shorty_len) {
399 char c = shorty[shorty_index];
400 shorty_index++;
Ian Rogersea2a11d2011-10-11 16:48:51 -0700401 if (c == 'L') {
Ian Rogers14b1b242011-10-11 18:54:34 -0700402 Object* obj = reinterpret_cast<Object*>(regs[cur_arg]);
Ian Rogersea2a11d2011-10-11 16:48:51 -0700403 AddLocalReference<jobject>(env, obj);
Ian Rogersad25ac52011-10-04 19:13:33 -0700404 }
Ian Rogersea2a11d2011-10-11 16:48:51 -0700405 cur_arg = cur_arg + (c == 'J' || c == 'D' ? 2 : 1);
Ian Rogersad25ac52011-10-04 19:13:33 -0700406 }
407 // Resolve method filling in dex cache
408 Method* called = linker->ResolveMethod(method_idx, *caller_sp, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700409 if (LIKELY(!thread->IsExceptionPending())) {
Ian Rogers573db4a2011-12-13 15:30:50 -0800410 if (LIKELY(called->IsDirect())) {
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800411 // Ensure that the called method's class is initialized
412 Class* called_class = called->GetDeclaringClass();
413 linker->EnsureInitialized(called_class, true);
414 if (LIKELY(called_class->IsInitialized())) {
415 // Update CodeAndDirectMethod table and avoid the trampoline when we know the called class
416 // is initialized (see test 084-class-init SlowInit)
417 Method* caller = *caller_sp;
418 DexCache* dex_cache = caller->GetDeclaringClass()->GetDexCache();
419 dex_cache->GetCodeAndDirectMethods()->SetResolvedDirectMethod(method_idx, called);
420 // We got this far, ensure that the declaring class is initialized
421 linker->EnsureInitialized(called->GetDeclaringClass(), true);
422 }
Ian Rogers573db4a2011-12-13 15:30:50 -0800423 } else {
424 // Direct method has been made virtual
425 thread->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
426 "Expected direct method but found virtual: %s",
427 PrettyMethod(called, true).c_str());
428 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700429 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700430 void* code;
Ian Rogerscaab8c42011-10-12 12:11:18 -0700431 if (UNLIKELY(thread->IsExceptionPending())) {
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700432 // Something went wrong in ResolveMethod or EnsureInitialized,
433 // go into deliver exception with the pending exception in r0
Ian Rogersad25ac52011-10-04 19:13:33 -0700434 code = reinterpret_cast<void*>(art_deliver_exception_from_code);
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700435 regs[0] = reinterpret_cast<uintptr_t>(thread->GetException());
Ian Rogersad25ac52011-10-04 19:13:33 -0700436 thread->ClearException();
437 } else {
438 // Expect class to at least be initializing
Ian Rogersbdfb1a52012-01-12 14:05:22 -0800439 DCHECK(called->GetDeclaringClass()->IsInitializing());
Ian Rogersad25ac52011-10-04 19:13:33 -0700440 // Set up entry into main method
Brian Carlstromb2062cf2011-11-03 01:24:44 -0700441 regs[0] = reinterpret_cast<uintptr_t>(called);
Ian Rogersad25ac52011-10-04 19:13:33 -0700442 code = const_cast<void*>(called->GetCode());
443 }
444 return code;
445}
446
Ian Rogerscaab8c42011-10-12 12:11:18 -0700447// Fast path field resolution that can't throw exceptions
448static Field* FindFieldFast(uint32_t field_idx, const Method* referrer) {
449 Field* resolved_field = referrer->GetDexCacheResolvedFields()->Get(field_idx);
450 if (UNLIKELY(resolved_field == NULL)) {
451 return NULL;
452 }
453 Class* fields_class = resolved_field->GetDeclaringClass();
454 // Check class is initilaized or initializing
455 if (UNLIKELY(!fields_class->IsInitializing())) {
456 return NULL;
457 }
458 return resolved_field;
459}
460
461// Slow path field resolution and declaring class initialization
Ian Rogersce9eca62011-10-07 17:11:03 -0700462Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static) {
463 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700464 Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
465 if (LIKELY(resolved_field != NULL)) {
466 Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogersce9eca62011-10-07 17:11:03 -0700467 // If the class is already initializing, we must be inside <clinit>, or
468 // we'd still be waiting for the lock.
Ian Rogerscaab8c42011-10-12 12:11:18 -0700469 if (fields_class->IsInitializing()) {
470 return resolved_field;
471 }
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700472 if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true)) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700473 return resolved_field;
Ian Rogersce9eca62011-10-07 17:11:03 -0700474 }
475 }
476 DCHECK(Thread::Current()->IsExceptionPending()); // Throw exception and unwind
477 return NULL;
478}
479
480extern "C" Field* artFindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer,
481 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700482 Field* resolved_field = FindFieldFast(field_idx, referrer);
483 if (UNLIKELY(resolved_field == NULL)) {
484 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
485 resolved_field = FindFieldFromCode(field_idx, referrer, false);
486 }
487 return resolved_field;
Ian Rogersce9eca62011-10-07 17:11:03 -0700488}
489
490extern "C" uint32_t artGet32StaticFromCode(uint32_t field_idx, const Method* referrer,
491 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700492 Field* field = FindFieldFast(field_idx, referrer);
493 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800494 FieldHelper fh(field);
495 if (LIKELY(fh.IsPrimitiveType() && fh.FieldSize() == sizeof(int32_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700496 return field->Get32(NULL);
497 }
498 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700499 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700500 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700501 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800502 FieldHelper fh(field);
503 if (!fh.IsPrimitiveType() || fh.FieldSize() != sizeof(int32_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700504 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
505 "Attempted read of 32-bit primitive on field '%s'",
506 PrettyField(field, true).c_str());
507 } else {
508 return field->Get32(NULL);
509 }
510 }
511 return 0; // Will throw exception by checking with Thread::Current
512}
513
514extern "C" uint64_t artGet64StaticFromCode(uint32_t field_idx, const Method* referrer,
515 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700516 Field* field = FindFieldFast(field_idx, referrer);
517 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800518 FieldHelper fh(field);
519 if (LIKELY(fh.IsPrimitiveType() && fh.FieldSize() == sizeof(int64_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700520 return field->Get64(NULL);
521 }
522 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700523 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700524 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700525 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800526 FieldHelper fh(field);
527 if (!fh.IsPrimitiveType() || fh.FieldSize() != sizeof(int64_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700528 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
529 "Attempted read of 64-bit primitive on field '%s'",
530 PrettyField(field, true).c_str());
531 } else {
532 return field->Get64(NULL);
533 }
534 }
535 return 0; // Will throw exception by checking with Thread::Current
536}
537
538extern "C" Object* artGetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
539 Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700540 Field* field = FindFieldFast(field_idx, referrer);
541 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800542 FieldHelper fh(field);
543 if (LIKELY(!fh.IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700544 return field->GetObj(NULL);
545 }
546 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700547 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700548 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700549 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800550 FieldHelper fh(field);
551 if (fh.IsPrimitiveType()) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700552 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
553 "Attempted read of reference on primitive field '%s'",
554 PrettyField(field, true).c_str());
555 } else {
556 return field->GetObj(NULL);
557 }
558 }
559 return NULL; // Will throw exception by checking with Thread::Current
560}
561
562extern "C" int artSet32StaticFromCode(uint32_t field_idx, const Method* referrer,
563 uint32_t new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700564 Field* field = FindFieldFast(field_idx, referrer);
565 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800566 FieldHelper fh(field);
567 if (LIKELY(fh.IsPrimitiveType() && fh.FieldSize() == sizeof(int32_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700568 field->Set32(NULL, new_value);
569 return 0; // success
570 }
571 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700572 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700573 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700574 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800575 FieldHelper fh(field);
576 if (!fh.IsPrimitiveType() || fh.FieldSize() != sizeof(int32_t)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700577 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
578 "Attempted write of 32-bit primitive to field '%s'",
579 PrettyField(field, true).c_str());
580 } else {
581 field->Set32(NULL, new_value);
582 return 0; // success
583 }
584 }
585 return -1; // failure
586}
587
588extern "C" int artSet64StaticFromCode(uint32_t field_idx, const Method* referrer,
589 uint64_t new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700590 Field* field = FindFieldFast(field_idx, referrer);
591 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800592 FieldHelper fh(field);
593 if (LIKELY(fh.IsPrimitiveType() && fh.FieldSize() == sizeof(int64_t))) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700594 field->Set64(NULL, new_value);
595 return 0; // success
596 }
597 }
598 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
599 field = FindFieldFromCode(field_idx, referrer, true);
600 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800601 FieldHelper fh(field);
602 if (UNLIKELY(!fh.IsPrimitiveType() || fh.FieldSize() != sizeof(int64_t))) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700603 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
604 "Attempted write of 64-bit primitive to field '%s'",
605 PrettyField(field, true).c_str());
606 } else {
607 field->Set64(NULL, new_value);
608 return 0; // success
609 }
610 }
611 return -1; // failure
612}
613
614extern "C" int artSetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
615 Object* new_value, Thread* self, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700616 Field* field = FindFieldFast(field_idx, referrer);
617 if (LIKELY(field != NULL)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800618 if (LIKELY(!FieldHelper(field).IsPrimitiveType())) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700619 field->SetObj(NULL, new_value);
620 return 0; // success
621 }
622 }
Ian Rogersce9eca62011-10-07 17:11:03 -0700623 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700624 field = FindFieldFromCode(field_idx, referrer, true);
Ian Rogersce9eca62011-10-07 17:11:03 -0700625 if (field != NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800626 if (FieldHelper(field).IsPrimitiveType()) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700627 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
628 "Attempted write of reference to primitive field '%s'",
629 PrettyField(field, true).c_str());
630 } else {
631 field->SetObj(NULL, new_value);
632 return 0; // success
633 }
634 }
635 return -1; // failure
636}
637
Shih-wei Liao2d831012011-09-28 22:06:53 -0700638// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
639// cannot be resolved, throw an error. If it can, use it to create an instance.
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800640// When verification/compiler hasn't been able to verify access, optionally perform an access
641// check.
642static Object* AllocObjectFromCode(uint32_t type_idx, Method* method, Thread* self,
643 bool access_check) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700644 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700645 Runtime* runtime = Runtime::Current();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700646 if (UNLIKELY(klass == NULL)) {
buzbee33a129c2011-10-06 16:53:20 -0700647 klass = runtime->GetClassLinker()->ResolveType(type_idx, method);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700648 if (klass == NULL) {
buzbee33a129c2011-10-06 16:53:20 -0700649 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700650 return NULL; // Failure
651 }
652 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800653 if (access_check) {
654 Class* referrer = method->GetDeclaringClass();
655 if (UNLIKELY(!referrer->CanAccess(klass))) {
656 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
657 "illegal class access: '%s' -> '%s'",
658 PrettyDescriptor(referrer).c_str(),
659 PrettyDescriptor(klass).c_str());
660 return NULL; // Failure
661 }
662 }
buzbee33a129c2011-10-06 16:53:20 -0700663 if (!runtime->GetClassLinker()->EnsureInitialized(klass, true)) {
664 DCHECK(self->IsExceptionPending());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700665 return NULL; // Failure
666 }
667 return klass->AllocObject();
668}
669
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800670extern "C" Object* artAllocObjectFromCode(uint32_t type_idx, Method* method,
671 Thread* self, Method** sp) {
672 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
673 return AllocObjectFromCode(type_idx, method, self, false);
674}
675
Ian Rogers28ad40d2011-10-27 15:19:26 -0700676extern "C" Object* artAllocObjectFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
677 Thread* self, Method** sp) {
678 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800679 return AllocObjectFromCode(type_idx, method, self, true);
680}
681
682// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
683// it cannot be resolved, throw an error. If it can, use it to create an array.
684// When verification/compiler hasn't been able to verify access, optionally perform an access
685// check.
686static Array* AllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
687 Thread* self, bool access_check) {
688 if (UNLIKELY(component_count < 0)) {
689 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d",
690 component_count);
691 return NULL; // Failure
692 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700693 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800694 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
695 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
696 if (klass == NULL) { // Error
697 DCHECK(Thread::Current()->IsExceptionPending());
698 return NULL; // Failure
699 }
700 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
701 }
702 if (access_check) {
703 Class* referrer = method->GetDeclaringClass();
704 if (UNLIKELY(!referrer->CanAccess(klass))) {
705 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
706 "illegal class access: '%s' -> '%s'",
707 PrettyDescriptor(referrer).c_str(),
708 PrettyDescriptor(klass).c_str());
Ian Rogers28ad40d2011-10-27 15:19:26 -0700709 return NULL; // Failure
710 }
711 }
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800712 return Array::Alloc(klass, component_count);
buzbeecc4540e2011-10-27 13:06:03 -0700713}
714
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800715extern "C" Array* artAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
716 Thread* self, Method** sp) {
717 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
718 return AllocArrayFromCode(type_idx, method, component_count, self, false);
719}
720
721extern "C" Array* artAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
722 int32_t component_count,
723 Thread* self, Method** sp) {
724 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
725 return AllocArrayFromCode(type_idx, method, component_count, self, true);
726}
727
728// Helper function to alloc array for OP_FILLED_NEW_ARRAY
Ian Rogersce9eca62011-10-07 17:11:03 -0700729Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count,
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800730 Thread* self, bool access_check) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700731 if (UNLIKELY(component_count < 0)) {
Ian Rogersce9eca62011-10-07 17:11:03 -0700732 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700733 return NULL; // Failure
734 }
735 Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700736 if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve
Shih-wei Liao2d831012011-09-28 22:06:53 -0700737 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
738 if (klass == NULL) { // Error
739 DCHECK(Thread::Current()->IsExceptionPending());
740 return NULL; // Failure
741 }
742 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700743 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700744 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700745 Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700746 "Bad filled array request for type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800747 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700748 } else {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700749 Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700750 "Found type %s; filled-new-array not implemented for anything but \'int\'",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800751 PrettyDescriptor(klass).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700752 }
753 return NULL; // Failure
754 } else {
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800755 if (access_check) {
756 Class* referrer = method->GetDeclaringClass();
757 if (UNLIKELY(!referrer->CanAccess(klass))) {
758 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
759 "illegal class access: '%s' -> '%s'",
760 PrettyDescriptor(referrer).c_str(),
761 PrettyDescriptor(klass).c_str());
762 return NULL; // Failure
763 }
764 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700765 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700766 return Array::Alloc(klass, component_count);
767 }
768}
769
Ian Rogersce9eca62011-10-07 17:11:03 -0700770extern "C" Array* artCheckAndAllocArrayFromCode(uint32_t type_idx, Method* method,
771 int32_t component_count, Thread* self, Method** sp) {
772 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800773 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, false);
Ian Rogersce9eca62011-10-07 17:11:03 -0700774}
775
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800776extern "C" Array* artCheckAndAllocArrayFromCodeWithAccessCheck(uint32_t type_idx, Method* method,
777 int32_t component_count,
778 Thread* self, Method** sp) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700779 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers0eb7d7e2012-01-31 21:12:32 -0800780 return CheckAndAllocArrayFromCode(type_idx, method, component_count, self, true);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700781}
782
Ian Rogerscaab8c42011-10-12 12:11:18 -0700783// Assignable test for code, won't throw. Null and equality tests already performed
784uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
785 DCHECK(klass != NULL);
786 DCHECK(ref_class != NULL);
787 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
788}
789
Shih-wei Liao2d831012011-09-28 22:06:53 -0700790// 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 -0700791extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700792 DCHECK(a->IsClass()) << PrettyClass(a);
793 DCHECK(b->IsClass()) << PrettyClass(b);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700794 if (LIKELY(b->IsAssignableFrom(a))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700795 return 0; // Success
796 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700797 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700798 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Shih-wei Liao2d831012011-09-28 22:06:53 -0700799 "%s cannot be cast to %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800800 PrettyDescriptor(a).c_str(),
801 PrettyDescriptor(b).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700802 return -1; // Failure
803 }
804}
805
806// Tests whether 'element' can be assigned into an array of type 'array_class'.
807// Returns 0 on success and -1 if an exception is pending.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700808extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
809 Thread* self, Method** sp) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700810 DCHECK(array_class != NULL);
811 // element can't be NULL as we catch this is screened in runtime_support
812 Class* element_class = element->GetClass();
813 Class* component_type = array_class->GetComponentType();
Ian Rogerscaab8c42011-10-12 12:11:18 -0700814 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
Shih-wei Liao2d831012011-09-28 22:06:53 -0700815 return 0; // Success
816 } else {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700817 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700818 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesd3127d62012-01-17 13:42:26 -0800819 "%s cannot be stored in an array of type %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800820 PrettyDescriptor(element_class).c_str(),
821 PrettyDescriptor(array_class).c_str());
Shih-wei Liao2d831012011-09-28 22:06:53 -0700822 return -1; // Failure
823 }
824}
825
Elliott Hughesf3778f62012-01-26 14:14:35 -0800826Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self,
827 bool can_run_clinit, bool verify_access) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700828 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
829 Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700830 if (UNLIKELY(klass == NULL)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700831 CHECK(self->IsExceptionPending());
832 return NULL; // Failure - Indicate to caller to deliver exception
833 }
Elliott Hughesf3778f62012-01-26 14:14:35 -0800834 // Perform access check if necessary.
835 if (verify_access && !referrer->GetDeclaringClass()->CanAccess(klass)) {
Ian Rogersb093c6b2011-10-31 16:19:55 -0700836 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
Elliott Hughesf3778f62012-01-26 14:14:35 -0800837 "Class %s is inaccessible to method %s",
838 PrettyDescriptor(klass).c_str(),
839 PrettyMethod(referrer, true).c_str());
840 return NULL; // Failure - Indicate to caller to deliver exception
841 }
842 // If we're just implementing const-class, we shouldn't call <clinit>.
843 if (!can_run_clinit) {
844 return klass;
Ian Rogersb093c6b2011-10-31 16:19:55 -0700845 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700846 // If we are the <clinit> of this class, just return our storage.
847 //
848 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
849 // running.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800850 if (klass == referrer->GetDeclaringClass() && MethodHelper(referrer).IsClassInitializer()) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700851 return klass;
852 }
853 if (!class_linker->EnsureInitialized(klass, true)) {
854 CHECK(self->IsExceptionPending());
855 return NULL; // Failure - Indicate to caller to deliver exception
856 }
857 referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
858 return klass;
Shih-wei Liao2d831012011-09-28 22:06:53 -0700859}
860
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700861extern "C" Class* artInitializeStaticStorageFromCode(uint32_t type_idx, const Method* referrer,
862 Thread* self, Method** sp) {
863 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800864 return ResolveVerifyAndClinit(type_idx, referrer, self, true, true);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700865}
866
Ian Rogers28ad40d2011-10-27 15:19:26 -0700867extern "C" Class* artInitializeTypeFromCode(uint32_t type_idx, const Method* referrer, Thread* self,
868 Method** sp) {
869 // Called when method->dex_cache_resolved_types_[] misses
870 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800871 return ResolveVerifyAndClinit(type_idx, referrer, self, false, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700872}
873
Ian Rogersb093c6b2011-10-31 16:19:55 -0700874extern "C" Class* artInitializeTypeAndVerifyAccessFromCode(uint32_t type_idx,
875 const Method* referrer, Thread* self,
876 Method** sp) {
877 // Called when caller isn't guaranteed to have access to a type and the dex cache may be
878 // unpopulated
879 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Elliott Hughesf3778f62012-01-26 14:14:35 -0800880 return ResolveVerifyAndClinit(type_idx, referrer, self, false, true);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700881}
882
buzbee48d72222012-01-11 15:19:51 -0800883// Helper function to resolve virtual method
884extern "C" Method* artResolveMethodFromCode(Method* referrer,
885 uint32_t method_idx,
886 bool is_direct,
887 Thread* self,
888 Method** sp) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700889 /*
890 * Slow-path handler on invoke virtual method path in which
buzbee48d72222012-01-11 15:19:51 -0800891 * base method is unresolved at compile-time. Caller will
892 * unwind if can't resolve.
Ian Rogers28ad40d2011-10-27 15:19:26 -0700893 */
buzbee48d72222012-01-11 15:19:51 -0800894 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
895 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
896 Method* method = class_linker->ResolveMethod(method_idx, referrer, is_direct);
897 referrer->GetDexCacheResolvedMethods()->Set(method_idx, method);
898 return method;
Ian Rogers28ad40d2011-10-27 15:19:26 -0700899}
900
Brian Carlstromaded5f72011-10-07 17:15:04 -0700901String* ResolveStringFromCode(const Method* referrer, uint32_t string_idx) {
902 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
903 return class_linker->ResolveString(string_idx, referrer);
904}
905
906extern "C" String* artResolveStringFromCode(Method* referrer, int32_t string_idx,
907 Thread* self, Method** sp) {
908 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
909 return ResolveStringFromCode(referrer, string_idx);
910}
911
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700912extern "C" int artUnlockObjectFromCode(Object* obj, Thread* self, Method** sp) {
913 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700914 DCHECK(obj != NULL); // Assumed to have been checked before entry
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700915 // MonitorExit may throw exception
916 return obj->MonitorExit(self) ? 0 /* Success */ : -1 /* Failure */;
917}
918
919extern "C" void artLockObjectFromCode(Object* obj, Thread* thread, Method** sp) {
920 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
921 DCHECK(obj != NULL); // Assumed to have been checked before entry
922 obj->MonitorEnter(thread); // May block
Shih-wei Liao2d831012011-09-28 22:06:53 -0700923 DCHECK(thread->HoldsLock(obj));
924 // Only possible exception is NPE and is handled before entry
925 DCHECK(!thread->IsExceptionPending());
926}
927
Ian Rogers4a510d82011-10-09 14:30:24 -0700928void CheckSuspendFromCode(Thread* thread) {
929 // Called when thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700930 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
931}
932
Ian Rogers4a510d82011-10-09 14:30:24 -0700933extern "C" void artTestSuspendFromCode(Thread* thread, Method** sp) {
934 // Called when suspend count check value is 0 and thread->suspend_count_ != 0
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700935 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700936 Runtime::Current()->GetThreadList()->FullSuspendCheck(thread);
937}
938
939/*
940 * Fill the array with predefined constant values, throwing exceptions if the array is null or
941 * not of sufficient length.
942 *
943 * NOTE: When dealing with a raw dex file, the data to be copied uses
944 * little-endian ordering. Require that oat2dex do any required swapping
945 * so this routine can get by with a memcpy().
946 *
947 * Format of the data:
948 * ushort ident = 0x0300 magic value
949 * ushort width width of each element in the table
950 * uint size number of elements in the table
951 * ubyte data[size*width] table of data values (may contain a single-byte
952 * padding at the end)
953 */
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700954extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table,
955 Thread* self, Method** sp) {
956 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700957 DCHECK_EQ(table[0], 0x0300);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700958 if (UNLIKELY(array == NULL)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700959 Thread::Current()->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
960 "null array in fill array");
Shih-wei Liao2d831012011-09-28 22:06:53 -0700961 return -1; // Error
962 }
963 DCHECK(array->IsArrayInstance() && !array->IsObjectArray());
964 uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700965 if (UNLIKELY(static_cast<int32_t>(size) > array->GetLength())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700966 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
967 "failed array fill. length=%d; index=%d", array->GetLength(), size);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700968 return -1; // Error
969 }
970 uint16_t width = table[1];
971 uint32_t size_in_bytes = size * width;
972 memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes);
973 return 0; // Success
974}
975
976// See comments in runtime_support_asm.S
977extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx,
Ian Rogerscaab8c42011-10-12 12:11:18 -0700978 Object* this_object,
979 Method* caller_method,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700980 Thread* thread, Method** sp) {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700981 Method* interface_method = caller_method->GetDexCacheResolvedMethods()->Get(method_idx);
982 Method* found_method = NULL; // The found method
983 if (LIKELY(interface_method != NULL && this_object != NULL)) {
Ian Rogersb04f69f2011-10-17 00:40:54 -0700984 found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, false);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700985 }
Ian Rogerscaab8c42011-10-12 12:11:18 -0700986 if (UNLIKELY(found_method == NULL)) {
987 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
988 if (this_object == NULL) {
989 thread->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
990 "null receiver during interface dispatch");
991 return 0;
992 }
993 if (interface_method == NULL) {
994 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
995 interface_method = class_linker->ResolveMethod(method_idx, caller_method, false);
996 if (interface_method == NULL) {
997 // Could not resolve interface method. Throw error and unwind
998 CHECK(thread->IsExceptionPending());
999 return 0;
1000 }
1001 }
Ian Rogersb04f69f2011-10-17 00:40:54 -07001002 found_method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method, true);
Ian Rogerscaab8c42011-10-12 12:11:18 -07001003 if (found_method == NULL) {
1004 CHECK(thread->IsExceptionPending());
1005 return 0;
1006 }
Shih-wei Liao2d831012011-09-28 22:06:53 -07001007 }
Ian Rogerscaab8c42011-10-12 12:11:18 -07001008 const void* code = found_method->GetCode();
Shih-wei Liao2d831012011-09-28 22:06:53 -07001009
Ian Rogerscaab8c42011-10-12 12:11:18 -07001010 uint32_t method_uint = reinterpret_cast<uint32_t>(found_method);
Shih-wei Liao2d831012011-09-28 22:06:53 -07001011 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1012 uint64_t result = ((code_uint << 32) | method_uint);
1013 return result;
1014}
1015
Ian Rogers466bb252011-10-14 03:29:56 -07001016static void ThrowNewUndeclaredThrowableException(Thread* self, JNIEnv* env, Throwable* exception) {
1017 ScopedLocalRef<jclass> jlr_UTE_class(env,
1018 env->FindClass("java/lang/reflect/UndeclaredThrowableException"));
1019 if (jlr_UTE_class.get() == NULL) {
1020 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1021 } else {
1022 jmethodID jlre_UTE_constructor = env->GetMethodID(jlr_UTE_class.get(), "<init>",
1023 "(Ljava/lang/Throwable;)V");
1024 jthrowable jexception = AddLocalReference<jthrowable>(env, exception);
1025 ScopedLocalRef<jthrowable> jlr_UTE(env,
1026 reinterpret_cast<jthrowable>(env->NewObject(jlr_UTE_class.get(), jlre_UTE_constructor,
1027 jexception)));
1028 int rc = env->Throw(jlr_UTE.get());
1029 if (rc != JNI_OK) {
1030 LOG(ERROR) << "Couldn't throw new \"java/lang/reflect/UndeclaredThrowableException\"";
1031 }
1032 }
1033 CHECK(self->IsExceptionPending());
1034}
1035
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001036// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
1037// which is responsible for recording callee save registers. We explicitly handlerize incoming
1038// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
1039// the invocation handler which is a field within the proxy object receiver.
1040extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
Ian Rogers466bb252011-10-14 03:29:56 -07001041 Thread* self, byte* stack_args) {
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001042 // Register the top of the managed stack
Ian Rogers466bb252011-10-14 03:29:56 -07001043 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
1044 DCHECK_EQ(*proxy_sp, proxy_method);
1045 self->SetTopOfStack(proxy_sp, 0);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001046 // TODO: ARM specific
1047 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
1048 // Start new JNI local reference state
1049 JNIEnvExt* env = self->GetJniEnv();
1050 ScopedJniEnvLocalRefState env_state(env);
1051 // Create local ref. copies of proxy method and the receiver
1052 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
1053 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
1054
Ian Rogers14b1b242011-10-11 18:54:34 -07001055 // Placing into local references incoming arguments from the caller's register arguments,
1056 // replacing original Object* with jobject
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001057 MethodHelper proxy_mh(proxy_method);
1058 const size_t num_params = proxy_mh.NumArgs();
Ian Rogers14b1b242011-10-11 18:54:34 -07001059 size_t args_in_regs = 0;
1060 for (size_t i = 1; i < num_params; i++) { // skip receiver
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001061 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001062 if (args_in_regs > 2) {
1063 args_in_regs = 2;
1064 break;
1065 }
1066 }
1067 size_t cur_arg = 0; // current stack location to read
1068 size_t param_index = 1; // skip receiver
1069 while (cur_arg < args_in_regs && param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001070 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001071 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001072 jobject jobj = AddLocalReference<jobject>(env, obj);
Ian Rogers14b1b242011-10-11 18:54:34 -07001073 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001074 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001075 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001076 param_index++;
1077 }
1078 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001079 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
Ian Rogers14b1b242011-10-11 18:54:34 -07001080 while (param_index < num_params) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001081 if (proxy_mh.IsParamAReference(param_index)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001082 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
1083 jobject jobj = AddLocalReference<jobject>(env, obj);
1084 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001085 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001086 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
Ian Rogers14b1b242011-10-11 18:54:34 -07001087 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001088 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001089 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
1090 jvalue args_jobj[3];
1091 args_jobj[0].l = rcvr_jobj;
1092 args_jobj[1].l = proxy_method_jobj;
Ian Rogers466bb252011-10-14 03:29:56 -07001093 // Args array, if no arguments then NULL (don't include receiver in argument count)
1094 args_jobj[2].l = NULL;
1095 ObjectArray<Object>* args = NULL;
1096 if ((num_params - 1) > 0) {
1097 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001098 if (args == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001099 CHECK(self->IsExceptionPending());
1100 return;
1101 }
1102 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
1103 }
1104 // Convert proxy method into expected interface method
1105 Method* interface_method = proxy_method->FindOverriddenMethod();
1106 CHECK(interface_method != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001107 CHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers466bb252011-10-14 03:29:56 -07001108 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001109 // Box arguments
Ian Rogers14b1b242011-10-11 18:54:34 -07001110 cur_arg = 0; // reset stack location to read to start
1111 // reset index, will index into param type array which doesn't include the receiver
1112 param_index = 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001113 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
Ian Rogers14b1b242011-10-11 18:54:34 -07001114 CHECK(param_types != NULL);
1115 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
1116 CHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
1117 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
1118 Class* param_type = param_types->Get(param_index);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001119 Object* obj;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001120 if (!param_type->IsPrimitive()) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001121 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001122 } else {
Ian Rogers14b1b242011-10-11 18:54:34 -07001123 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
1124 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
1125 // long/double split over regs and stack, mask in high half from stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001126 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
Ian Rogerscaab8c42011-10-12 12:11:18 -07001127 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
Ian Rogers14b1b242011-10-11 18:54:34 -07001128 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001129 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001130 if (self->IsExceptionPending()) {
1131 return;
1132 }
1133 obj = val.l;
1134 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001135 args->Set(param_index, obj);
1136 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1137 param_index++;
1138 }
1139 // Placing into local references incoming arguments from the caller's stack arguments
Ian Rogers466bb252011-10-14 03:29:56 -07001140 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
1141 while (param_index < (num_params - 1)) {
Ian Rogers14b1b242011-10-11 18:54:34 -07001142 Class* param_type = param_types->Get(param_index);
1143 Object* obj;
1144 if (!param_type->IsPrimitive()) {
1145 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
1146 } else {
1147 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001148 BoxPrimitive(env, param_type->GetPrimitiveType(), val);
Ian Rogers14b1b242011-10-11 18:54:34 -07001149 if (self->IsExceptionPending()) {
1150 return;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001151 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001152 obj = val.l;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001153 }
Ian Rogers14b1b242011-10-11 18:54:34 -07001154 args->Set(param_index, obj);
1155 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
1156 param_index++;
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001157 }
1158 // Get the InvocationHandler method and the field that holds it within the Proxy object
1159 static jmethodID inv_hand_invoke_mid = NULL;
1160 static jfieldID proxy_inv_hand_fid = NULL;
1161 if (proxy_inv_hand_fid == NULL) {
Ian Rogers466bb252011-10-14 03:29:56 -07001162 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001163 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
Ian Rogers466bb252011-10-14 03:29:56 -07001164 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001165 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
1166 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
1167 }
Ian Rogers466bb252011-10-14 03:29:56 -07001168 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001169 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
1170 // Call InvocationHandler.invoke
1171 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
1172 // Place result in stack args
1173 if (!self->IsExceptionPending()) {
1174 Object* result_ref = self->DecodeJObject(result);
1175 if (result_ref != NULL) {
1176 JValue result_unboxed;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001177 UnboxPrimitive(env, result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001178 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
1179 } else {
1180 *reinterpret_cast<jobject*>(stack_args) = NULL;
1181 }
Ian Rogers466bb252011-10-14 03:29:56 -07001182 } else {
1183 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
1184 // a UndeclaredThrowableException.
1185 Throwable* exception = self->GetException();
1186 self->ClearException();
1187 if (!exception->IsCheckedException()) {
1188 self->SetException(exception);
1189 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -08001190 SynthesizedProxyClass* proxy_class =
1191 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
1192 int throws_index = -1;
1193 size_t num_virt_methods = proxy_class->NumVirtualMethods();
1194 for (size_t i = 0; i < num_virt_methods; i++) {
1195 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
1196 throws_index = i;
1197 break;
1198 }
1199 }
1200 CHECK_NE(throws_index, -1);
1201 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
Ian Rogers466bb252011-10-14 03:29:56 -07001202 Class* exception_class = exception->GetClass();
1203 bool declares_exception = false;
1204 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
1205 Class* declared_exception = declared_exceptions->Get(i);
1206 declares_exception = declared_exception->IsAssignableFrom(exception_class);
1207 }
1208 if (declares_exception) {
1209 self->SetException(exception);
1210 } else {
1211 ThrowNewUndeclaredThrowableException(self, env, exception);
1212 }
1213 }
Ian Rogersdfcdf1a2011-10-10 17:50:35 -07001214 }
1215}
1216
jeffhaoe343b762011-12-05 16:36:44 -08001217extern "C" const void* artTraceMethodEntryFromCode(Method* method, Thread* self, uintptr_t lr) {
jeffhao2692b572011-12-16 15:42:28 -08001218 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001219 TraceStackFrame trace_frame = TraceStackFrame(method, lr);
1220 self->PushTraceStackFrame(trace_frame);
1221
jeffhao2692b572011-12-16 15:42:28 -08001222 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceEnter);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001223
jeffhao2692b572011-12-16 15:42:28 -08001224 return tracer->GetSavedCodeFromMap(method);
jeffhaoe343b762011-12-05 16:36:44 -08001225}
1226
1227extern "C" uintptr_t artTraceMethodExitFromCode() {
jeffhao2692b572011-12-16 15:42:28 -08001228 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001229 TraceStackFrame trace_frame = Thread::Current()->PopTraceStackFrame();
1230 Method* method = trace_frame.method_;
1231 uintptr_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001232
jeffhao2692b572011-12-16 15:42:28 -08001233 tracer->LogMethodTraceEvent(Thread::Current(), method, Trace::kMethodTraceExit);
jeffhaoe343b762011-12-05 16:36:44 -08001234
1235 return lr;
1236}
1237
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001238uint32_t artTraceMethodUnwindFromCode(Thread* self) {
jeffhao2692b572011-12-16 15:42:28 -08001239 Trace* tracer = Runtime::Current()->GetTracer();
jeffhaoe343b762011-12-05 16:36:44 -08001240 TraceStackFrame trace_frame = self->PopTraceStackFrame();
1241 Method* method = trace_frame.method_;
Elliott Hughesad6c9c32012-01-19 17:39:12 -08001242 uint32_t lr = trace_frame.return_pc_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001243
jeffhao2692b572011-12-16 15:42:28 -08001244 tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind);
jeffhaoe343b762011-12-05 16:36:44 -08001245
1246 return lr;
1247}
1248
Shih-wei Liao2d831012011-09-28 22:06:53 -07001249/*
1250 * Float/double conversion requires clamping to min and max of integer form. If
1251 * target doesn't support this normally, use these.
1252 */
1253int64_t D2L(double d) {
1254 static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL;
1255 static const double kMinLong = (double)(int64_t)0x8000000000000000ULL;
1256 if (d >= kMaxLong)
1257 return (int64_t)0x7fffffffffffffffULL;
1258 else if (d <= kMinLong)
1259 return (int64_t)0x8000000000000000ULL;
1260 else if (d != d) // NaN case
1261 return 0;
1262 else
1263 return (int64_t)d;
1264}
1265
1266int64_t F2L(float f) {
1267 static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL;
1268 static const float kMinLong = (float)(int64_t)0x8000000000000000ULL;
1269 if (f >= kMaxLong)
1270 return (int64_t)0x7fffffffffffffffULL;
1271 else if (f <= kMinLong)
1272 return (int64_t)0x8000000000000000ULL;
1273 else if (f != f) // NaN case
1274 return 0;
1275 else
1276 return (int64_t)f;
1277}
1278
1279} // namespace art